国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 仿微信自定義數字鍵盤的實現代碼

2019-12-12 02:26:41
字體:
來源:轉載
供稿:網友

本文介紹了Android 仿微信自定義數字鍵盤的實現代碼,分享給大家,希望對大家有幫助

最終效果:

實現這個自定義鍵盤的思路很簡單:

  1. 要寫出一個數字鍵盤的布局;
  2. 與 Edittext 結合使用,對每個按鍵的點擊事件進行處理;
  3. 禁用系統軟鍵盤。

有了思路,實現起來就不難了。

1. 實現鍵盤的 xml 布局

網格樣式的布局用 GridView 或者 RecyclerView 都可以實現,其實用 GridView 更方便一些,不過我為了多熟悉 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 用來實現鍵盤布局,上面的 RelativeLayout 則是為了實現收起鍵盤的點擊事件。

2. 在代碼中實現鍵盤布局,填充數據、增加點擊事件

我們新建類 KeyboardView 繼承自 RelativeLayout,關聯上面的布局文件,然后做一些初始化操作:對 RecyclerView 填充數據、設置適配器,設置出現和消失的動畫效果,寫一些會用到的方法等。

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) { // 點擊關閉鍵盤        dismiss();      }    });    recyclerView = findViewById(R.id.recycler_view);    initData();    initView();    initAnimation();  }  // 填充數據  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("");      }    }  }  // 設置適配器  private void initView() {    recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));    adapter = new KeyboardAdapter(getContext(), datas);    recyclerView.setAdapter(adapter);  }  // 初始化動畫效果  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);  }  // 關閉軟鍵盤  public void dismiss() {    if (isVisible()) {      startAnimation(animationOut);      setVisibility(GONE);    }  }  // 判斷軟鍵盤的狀態  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 里面都是很簡單的代碼,這里就不貼出了,文章末尾我會給出源碼下載地址。

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

3. 與 Edittext 結合使用

1. 禁用系統軟鍵盤

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();   }}

在網上找了一些方法,但是點擊 Edittext 的時候系統軟鍵盤依然會彈出。最后找到了這個方法,利用反射強制不彈出軟鍵盤,效果不錯。

2. 處理各個按鍵的點擊事件

  @Override  public void onKeyClick(View view, RecyclerView.ViewHolder holder, int position) {    switch (position) {      case 9: // 按下小數點        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: // 按下數字鍵        if ("0".equals(etInput.getText().toString().trim())) { // 第一個數字按下0的話,第二個數字只能按小數點          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) {    // 點擊刪除按鈕    String num = etInput.getText().toString().trim();    if (num.length() > 0) {      etInput.setText(num.substring(0, num.length() - 1));      etInput.setSelection(etInput.getText().length());    }  }

邏輯也非常簡單,看代碼就明白了。最終的效果就是第一張圖的樣子。

這個鍵盤很簡單,打算之后寫一個模仿微信或者支付寶的支付密碼輸入布局。

->->->點擊下載源碼<-<-<-

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 廉江市| 花垣县| 麦盖提县| 宜州市| 南投市| 正定县| 托里县| 高淳县| 宁海县| 洪江市| 平塘县| 灌云县| 甘肃省| 岑溪市| 文山县| 荔浦县| 叙永县| 元氏县| 壤塘县| 鄯善县| 德庆县| 仪陇县| 资兴市| 福清市| 定日县| 伊川县| 满城县| 南川市| 肇州县| 泸定县| 磐安县| 洱源县| 山丹县| 桓台县| 古蔺县| 诸暨市| 阿瓦提县| 德保县| 比如县| 简阳市| 尉氏县|