Android中如何自定義一個(gè)數(shù)字鍵盤-創(chuàng)新互聯(lián)

這篇文章主要介紹“Android中如何自定義一個(gè)數(shù)字鍵盤”,在日常操作中,相信很多人在Android中如何自定義一個(gè)數(shù)字鍵盤問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android中如何自定義一個(gè)數(shù)字鍵盤”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

10年積累的網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有曲阜免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

1. 實(shí)現(xiàn)鍵盤的 xml 布局

網(wǎng)格樣式的布局用 GridView 或者 RecyclerView 都可以實(shí)現(xiàn),其實(shí)用 GridView 更方便一些,不過(guò)我為了多熟悉 RecyclerView 的用法,這里選擇用了 RecyclerView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">

  <View
    android:layout_width="match_parent"
    android:layout_height="2px"
    android:background="@color/btn_gray"/>

  <RelativeLayout
    android:id="@+id/rl_back"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/iv_back_bg"
    android:padding="10dp">

    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:src="@mipmap/keyboard_back"/>
  </RelativeLayout>

  <View
    android:layout_width="match_parent"
    android:layout_height="1px"
    android:background="@color/btn_gray"/>

  <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/keyboard_bg"
    android:overScrollMode="never"></android.support.v7.widget.RecyclerView>

</LinearLayout>

RecyclerView 用來(lái)實(shí)現(xiàn)鍵盤布局,上面的 RelativeLayout 則是為了實(shí)現(xiàn)收起鍵盤的點(diǎn)擊事件。

2. 在代碼中實(shí)現(xiàn)鍵盤布局,填充數(shù)據(jù)、增加點(diǎn)擊事件

我們新建類 KeyboardView 繼承自 RelativeLayout,關(guān)聯(lián)上面的布局文件,然后做一些初始化操作:對(duì) RecyclerView 填充數(shù)據(jù)、設(shè)置適配器,設(shè)置出現(xiàn)和消失的動(dòng)畫效果,寫一些會(huì)用到的方法等。

public class KeyboardView extends RelativeLayout {

  private RelativeLayout rlBack;
  private RecyclerView recyclerView;
  private List<String> datas;
  private KeyboardAdapter adapter;
  private Animation animationIn;
  private Animation animationOut;


  public KeyboardView(Context context) {
    this(context, null);
  }

  public KeyboardView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
  }

  private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    LayoutInflater.from(context).inflate(R.layout.layout_key_board, this);
    rlBack = findViewById(R.id.rl_back);
    rlBack.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) { // 點(diǎn)擊關(guān)閉鍵盤
        dismiss();
      }
    });
    recyclerView = findViewById(R.id.recycler_view);

    initData();
    initView();
    initAnimation();
  }

  // 填充數(shù)據(jù)
  private void initData() {
    datas = new ArrayList<>();
    for (int i = 0; i < 12; i++) {
      if (i < 9) {
        datas.add(String.valueOf(i + 1));
      } else if (i == 9) {
        datas.add(".");
      } else if (i == 10) {
        datas.add("0");
      } else {
        datas.add("");
      }
    }
  }

  // 設(shè)置適配器
  private void initView() {
    recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
    adapter = new KeyboardAdapter(getContext(), datas);
    recyclerView.setAdapter(adapter);
  }

  // 初始化動(dòng)畫效果
  private void initAnimation() {
    animationIn = AnimationUtils.loadAnimation(getContext(), R.anim.keyboard_in);
    animationOut = AnimationUtils.loadAnimation(getContext(), R.anim.keyboard_out);
  }

  // 彈出軟鍵盤
  public void show() {
    startAnimation(animationIn);
    setVisibility(VISIBLE);
  }

  // 關(guān)閉軟鍵盤
  public void dismiss() {
    if (isVisible()) {
      startAnimation(animationOut);
      setVisibility(GONE);
    }
  }

  // 判斷軟鍵盤的狀態(tài)
  public boolean isVisible() {
    if (getVisibility() == VISIBLE) {
      return true;
    }
    return false;
  }

  public void setOnKeyBoardClickListener(KeyboardAdapter.OnKeyboardClickListener listener) {
    adapter.setOnKeyboardClickListener(listener);
  }

  public List<String> getDatas() {
    return datas;
  }

  public RelativeLayout getRlBack() {
    return rlBack;
  }
}

Adapter 里面都是很簡(jiǎn)單的代碼,這里就不貼出了,文章末尾我會(huì)給出源碼下載地址。

到這里為止,自定義數(shù)字鍵盤基本就算寫好了,不過(guò)最重要的還是要和 Edittext 結(jié)合使用。

3. 與 Edittext 結(jié)合使用

1. 禁用系統(tǒng)軟鍵盤

if (Build.VERSION.SDK_INT <= 10) {
   etInput.setInputType(InputType.TYPE_NULL);
} else {
   getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
   try {
     Class<EditText> cls = EditText.class;
     Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
     setShowSoftInputOnFocus.setAccessible(true);
     setShowSoftInputOnFocus.invoke(etInput, false);
   } catch (Exception e) {
     e.printStackTrace();
   }
}

在網(wǎng)上找了一些方法,但是點(diǎn)擊 Edittext 的時(shí)候系統(tǒng)軟鍵盤依然會(huì)彈出。最后找到了這個(gè)方法,利用反射強(qiáng)制不彈出軟鍵盤,效果不錯(cuò)。

2. 處理各個(gè)按鍵的點(diǎn)擊事件

  @Override
  public void onKeyClick(View view, RecyclerView.ViewHolder holder, int position) {
    switch (position) {
      case 9: // 按下小數(shù)點(diǎn)
        String num = etInput.getText().toString().trim();
        if (!num.contains(datas.get(position))) {
          num += datas.get(position);
          etInput.setText(num);
          etInput.setSelection(etInput.getText().length());
        }
        break;
      default: // 按下數(shù)字鍵
        if ("0".equals(etInput.getText().toString().trim())) { // 第一個(gè)數(shù)字按下0的話,第二個(gè)數(shù)字只能按小數(shù)點(diǎn)
          break;
        }
        etInput.setText(etInput.getText().toString().trim() + datas.get(position));
        etInput.setSelection(etInput.getText().length());
        break;
    }
  }

  @Override
  public void onDeleteClick(View view, RecyclerView.ViewHolder holder, int position) {
    // 點(diǎn)擊刪除按鈕
    String num = etInput.getText().toString().trim();
    if (num.length() > 0) {
      etInput.setText(num.substring(0, num.length() - 1));
      etInput.setSelection(etInput.getText().length());
    }
  }

到此,關(guān)于“Android中如何自定義一個(gè)數(shù)字鍵盤”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

文章名稱:Android中如何自定義一個(gè)數(shù)字鍵盤-創(chuàng)新互聯(lián)
本文路徑:http://www.bm7419.com/article26/dgcijg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序網(wǎng)站收錄、小程序開發(fā)、網(wǎng)站策劃、企業(yè)網(wǎng)站制作網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名