android 自定義鍵盤
剛剛做了個自定義鍵盤,方法記錄下來以便以後用到參考,介面主要有一個editText和鍵盤。剛開始的時候我是用PopupWindow做的軟鍵盤,後來發現不好用,因為PopupWindow和
editText不在一個層,每次點選鍵盤時EditText就沒有焦點,
如果我輸入了123456,現在想在2後面輸入一個小數點,結果還要點選一下editText才能獲得焦點,而此時popupWindow鍵盤會
消失,想輸入只有再次點選editText後,popupWindow鍵盤才能顯示出來,最後也沒有找到解決的辦法,所以我用了第二種方法比較簡單。相對佈局把鍵盤固定在最底部,別的不多說,先上圖再上程式碼吧。
一、這是效果圖,比較醜。。。
二、這個是自定義鍵盤的xml文件,comm_keyboard.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:id="@+id/keyboard_layout" android:layout_width="match_parent" android:layout_height="225dip" android:layout_alignParentBottom="true" android:background="#f6f6f7" android:orientation="horizontal" > <TableLayout android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" > <TableRow> <Button android:id="@+id/custom_keyboard_btn1" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="1" android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn2" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="2" android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn3" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="3" android:textColor="#000000" android:textSize="30sp" /> </TableRow> <TableRow> <Button android:id="@+id/custom_keyboard_btn4" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="4" android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn5" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="5" android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn6" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="6" android:textColor="#000000" android:textSize="30sp" /> </TableRow> <TableRow> <Button android:id="@+id/custom_keyboard_btn7" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="7" android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn8" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="8" android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn9" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="9" android:textColor="#000000" android:textSize="30sp" /> </TableRow> <TableRow> <Button android:id="@+id/custom_keyboard_btn_point" android:layout_width="0dip" android:layout_height="59dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="." android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn0" android:layout_width="0dip" android:layout_height="59dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="0" android:textColor="#000000" android:textSize="30sp" /> <Button android:id="@+id/custom_keyboard_btn_system_keyboard" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="系統鍵" android:textColor="#000000" android:textSize="15sp" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="90dip" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/custom_keyboard_btn_delete" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="刪除" android:textColor="#000000" android:textSize="20sp" /> <Button android:id="@+id/custom_keyboard_btn_sure" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@drawable/table_line_gray" android:gravity="center" android:text="確定" android:textSize="20sp" /> </LinearLayout> </LinearLayout> </LinearLayout>
這是activity_main.xml文件
<RelativeLayout xmlns:android="" xmlns:tools="" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/input_lay" android:layout_width="match_parent" android:layout_height="50dip" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="金額:" /> <EditText android:id="@+id/ed_write_money" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入金額" /> </LinearLayout> <LinearLayout android:id="@+id/bottom_lay" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <include layout="@layout/comm_keyboard" /> </LinearLayout> </RelativeLayout>
這是自定義鍵盤Activity,,CustomKeyboardActivity
package com.example.customkeyboard; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.text.Editable; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.view.ViewGroup; import android.view.Window; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.PopupWindow; /** * 自定義鍵盤 * */ public class CustomKeyboardActivity extends Activity implements OnFocusChangeListener { private static final String TAG = CustomKeyboardActivity.class.getName(); public static PopupWindow popupWindow = null; private String str = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); initView(); initData(); } protected void initData() { } protected void initView() { } /** * 顯示公共的自定義鍵盤 * * @param context * @param ed */ public void showKeyBoard(Context context, final EditText ed) { try { Button btn0 = (Button) findViewById(R.id.custom_keyboard_btn0); Button btn1 = (Button) findViewById(R.id.custom_keyboard_btn1); Button btn2 = (Button) findViewById(R.id.custom_keyboard_btn2); Button btn3 = (Button) findViewById(R.id.custom_keyboard_btn3); Button btn4 = (Button) findViewById(R.id.custom_keyboard_btn4); Button btn5 = (Button) findViewById(R.id.custom_keyboard_btn5); Button btn6 = (Button) findViewById(R.id.custom_keyboard_btn6); Button btn7 = (Button) findViewById(R.id.custom_keyboard_btn7); Button btn8 = (Button) findViewById(R.id.custom_keyboard_btn8); Button btn9 = (Button) findViewById(R.id.custom_keyboard_btn9); Button pointBtn = (Button) findViewById(R.id.custom_keyboard_btn_point); Button system_broad_btn = (Button) findViewById(R.id.custom_keyboard_btn_system_keyboard); Button deleteBtn = (Button) findViewById(R.id.custom_keyboard_btn_delete); Button sureBtn = (Button) findViewById(R.id.custom_keyboard_btn_sure); btn0.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart();// 獲得游標位置 Editable editable = ed.getText(); editable.insert(index, "0"); } }); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "1"); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "2"); } }); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "3"); } }); btn4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "4"); } }); btn5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "5"); } }); btn6.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "6"); } }); btn7.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "7"); } }); btn8.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "8"); } }); btn9.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "9"); } }); pointBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int index = ed.getSelectionStart(); Editable editable = ed.getText(); editable.insert(index, "."); } }); deleteBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str = ed.getText().toString(); if (str.length() > 0) { int index = ed.getSelectionStart();// 獲取游標位置 Editable editable = ed.getText(); editable.delete(index - 1, index); } } }); system_broad_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "系統鍵"); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); } }); sureBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "確定"); } }); } catch (Exception e) { e.printStackTrace(); } } @Override public void onFocusChange(View v, boolean hasFocus) { } } 這是MainActivity,繼承CustomKeyboardActivity package com.example.customkeyboard; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class MainActivity extends CustomKeyboardActivity implements OnClickListener { private RelativeLayout parent = null; private EditText inputMoney; private LinearLayout inputLayout; @Override protected void initView() { setContentView(R.layout.activity_main); parent = (RelativeLayout) findViewById(R.id.main); inputLayout = (LinearLayout) findViewById(R.id.input_lay); inputMoney = (EditText) findViewById(R.id.ed_write_money); inputLayout = (LinearLayout) findViewById(R.id.input_lay); inputMoney.setOnClickListener(this); showKeyBoard(MainActivity.this, inputMoney); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ed_write_money: // ShowPopupWindow(MainActivity.this, inputMoney, inputLayout); break; } } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3349/viewspace-2816730/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- android自定義鍵盤 自定義身份證鍵盤Android
- 自定義Android鍵盤Android
- android自定義view(自定義數字鍵盤)AndroidView
- Android自定義數字鍵盤Android
- Android自定義收銀鍵盤Android
- Android自定義view之emoji鍵盤AndroidView
- vue自定義鍵盤Vue
- 自定義鍵盤(一)
- 自定義鍵盤(二)
- 自定義身份證鍵盤(Swift)Swift
- Swift自定義表情鍵盤+錄音Swift
- iOS 自定義鍵盤字母按鈕iOS
- Android 最簡單的自定義數字鍵盤之一Android
- Android自定義幸運轉盤Android
- 使用 React Native 自定義安全鍵盤(iOS 和 Android 雙適配)React NativeiOSAndroid
- 一個高仿閒魚鍵自定義數字鍵盤特效特效
- Android - JoystickView 虛擬手柄,控制盤,自定義UIAndroidJoystickView虛擬手柄UI
- 自定義儀表盤
- React Native 自定義鍵盤之輸入車牌號React Native
- 我勸你別在數字鍵盤上刁難我——Vue自定義數字鍵盤元件Vue元件
- word自定義快捷鍵
- windows10系統怎麼建立自定義鍵盤佈局Windows
- Flutter仿微信,支付寶密碼輸入框+自定義鍵盤Flutter密碼
- Hibernate 自定義主鍵
- 自定義右鍵選單
- 自定義 Android 鐘表盤,這一篇就夠了Android
- 小米手環錶盤自定義
- 移動端頁面自定義input喚起鍵盤return,換行鍵為搜尋
- Android 自定義viewAndroidView
- Android 自定義 TabLayoutAndroidTabLayout
- Android: 自定義ViewAndroidView
- Android自定義ToastAndroidAST
- Android 自定義 DrawableAndroid
- android自定義View&自定義ViewGroup(下)AndroidView
- android自定義View&自定義ViewGroup(上)AndroidView
- Android自定義控制元件——自定義屬性Android控制元件
- vue 基礎入門筆記 08:鍵盤修飾符、自定義指令Vue筆記
- Android自定義控制元件之自定義屬性Android控制元件