先看一下键盘的布局文件control_digitpasswordkeypad.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/digitkeypadrootlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/transpwdpdpanel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#000000" > </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" android:background="@drawable/keyboardbackground" android:gravity="bottom" android:orientation="vertical" android:paddingBottom="4dp" android:paddingLeft="4dp" android:paddingRight="4dp" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:orientation="horizontal" > <EditText android:id="@+id/digitpadedittext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|right" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/digitkeypad_1" android:layout_width="wrap_content" android:layout_height="46dp" android:layout_marginLeft="4dp" android:layout_marginRight="2dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:gravity="center" android:text="1" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_2" android:layout_width="wrap_content" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="3dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:gravity="center" android:text="2" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_3" android:layout_width="wrap_content" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="4dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:gravity="center" android:text="3" android:textSize="22sp" > </Button> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/digitkeypad_4" android:layout_width="wrap_content" android:layout_height="46dp" android:layout_marginLeft="4dp" android:layout_marginRight="2dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:text="4" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_5" android:layout_width="wrap_content" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="3dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:text="5" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_6" android:layout_width="wrap_content" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="4dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:text="6" android:textSize="22sp" > </Button> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/digitkeypad_7" android:layout_width="80dp" android:layout_height="46dp" android:layout_marginLeft="4dp" android:layout_marginRight="2dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:text="7" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_8" android:layout_width="80dp" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="3dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:text="8" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_9" android:layout_width="80dp" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="4dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:text="9" android:textSize="22sp" > </Button> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/digitkeypad_c" android:layout_width="80dp" android:layout_height="46dp" android:layout_marginLeft="4dp" android:layout_marginRight="2dp" android:layout_weight="1" android:background="@drawable/keyboardclear" android:text="" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_0" android:layout_width="80dp" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="3dp" android:layout_weight="1" android:background="@drawable/widget_keypad_digit" android:text="0" android:textSize="22sp" > </Button> <Button android:id="@+id/digitkeypad_ok" android:layout_width="80dp" android:layout_height="46dp" android:layout_marginLeft="2dp" android:layout_marginRight="4dp" android:layout_weight="1" android:background="@drawable/keyboardok" android:text="" android:textSize="22sp" > </Button> </LinearLayout> </LinearLayout> </LinearLayout>
键盘布局文件截图如下:
下面看一下主界面的布局文件mian.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入数字" /> </LinearLayout>
主界面只有一个TextView控件,通过触发TextView的单击事件弹出自定义键盘
触发事件代码:
- TextView editText = (TextView)findViewById(R.id.input);
- editText.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showPassWdPadView();
- }
- });
弹出自定义键盘方法showPassWdPadView()
- private void showPassWdPadView() {
- this.runOnUiThread(new Runnable() {
- public void run() {
- // 让一个视图浮动在你的应用程序之上
- WindowManager windowmanager = (WindowManager) content.getSystemService(Context.WINDOW_SERVICE);
- LayoutParams layoutparams = new LayoutParams(-1, -1, WindowManager.LayoutParams.FIRST_SUB_WINDOW, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888);
- layoutparams.gravity = Gravity.BOTTOM;
- passwdview.findViewById(R.id.transpwdpdpanel).getBackground().setAlpha(140);
- windowmanager.addView(passwdview, layoutparams);
- }
- });
- }
这里用到了WindowManager,具体用法可参考网上。
下面是主界面的全部代码
import android.app.Activity; import android.content.Context; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.WindowManager.LayoutParams; import android.widget.TextView; public class AndroidInputNumberActivity extends Activity { private DigitPasswordKeyPad dpk; private View passwdview; private Context content ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); content = this; dpk = new DigitPasswordKeyPad(this); passwdview = dpk.setup(); TextView editText = (TextView)findViewById(R.id.input); editText.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showPassWdPadView(); } }); } private void showPassWdPadView() { this.runOnUiThread(new Runnable() { public void run() { // 让一个视图浮动在你的应用程序之上 WindowManager windowmanager = (WindowManager) content.getSystemService(Context.WINDOW_SERVICE); LayoutParams layoutparams = new LayoutParams(-1, -1, WindowManager.LayoutParams.FIRST_SUB_WINDOW, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888); layoutparams.gravity = Gravity.BOTTOM; passwdview.findViewById(R.id.transpwdpdpanel).getBackground().setAlpha(140); windowmanager.addView(passwdview, layoutparams); } }); } }
面代码中passwdview这个对象就是弹出键盘界面的View实例,passwdview这个对象是在DigitPasswordKeyPad中初始化得到。
DigitPasswordKeyPad类自定义键盘类,继承自View类
import android.content.Context; import android.text.InputType; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; /** * 系统定制的数字密码键盘 * * @author twf * */ public class DigitPasswordKeyPad extends View { private Context ctx = null; private View v; private String digitnum = ""; private int length = 6; private Button digitkeypad_1; private Button digitkeypad_2; private Button digitkeypad_3; private Button digitkeypad_4; private Button digitkeypad_5; private Button digitkeypad_6; private Button digitkeypad_7; private Button digitkeypad_8; private Button digitkeypad_9; private Button digitkeypad_0; private Button digitkeypad_c; private Button digitkeypad_ok; private EditText digitkeypad_edittext; private boolean isPwd; public DigitPasswordKeyPad(Context ctx) { super(ctx); this.ctx = ctx; } @Override protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { } public void setEditTextIsPwd(boolean ispwd) { if (ispwd) { digitkeypad_edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); } else { digitkeypad_edittext.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); } this.isPwd = ispwd; } public View setup() { LayoutInflater lif = LayoutInflater.from(ctx); v = lif.inflate(R.layout.control_digitpasswordkeypad, null); // 初始化 对象 digitkeypad_1 = (Button) v.findViewById(R.id.digitkeypad_1); digitkeypad_2 = (Button) v.findViewById(R.id.digitkeypad_2); digitkeypad_3 = (Button) v.findViewById(R.id.digitkeypad_3); digitkeypad_4 = (Button) v.findViewById(R.id.digitkeypad_4); digitkeypad_5 = (Button) v.findViewById(R.id.digitkeypad_5); digitkeypad_6 = (Button) v.findViewById(R.id.digitkeypad_6); digitkeypad_7 = (Button) v.findViewById(R.id.digitkeypad_7); digitkeypad_8 = (Button) v.findViewById(R.id.digitkeypad_8); digitkeypad_9 = (Button) v.findViewById(R.id.digitkeypad_9); digitkeypad_0 = (Button) v.findViewById(R.id.digitkeypad_0); digitkeypad_c = (Button) v.findViewById(R.id.digitkeypad_c); digitkeypad_ok = (Button) v.findViewById(R.id.digitkeypad_ok); digitkeypad_edittext = (EditText) v.findViewById(R.id.digitpadedittext); // 添加点击事件 DigitPasswordKeypadOnClickListener dkol = new DigitPasswordKeypadOnClickListener(); digitkeypad_1.setOnClickListener(dkol); digitkeypad_2.setOnClickListener(dkol); digitkeypad_3.setOnClickListener(dkol); digitkeypad_4.setOnClickListener(dkol); digitkeypad_5.setOnClickListener(dkol); digitkeypad_6.setOnClickListener(dkol); digitkeypad_7.setOnClickListener(dkol); digitkeypad_8.setOnClickListener(dkol); digitkeypad_9.setOnClickListener(dkol); digitkeypad_0.setOnClickListener(dkol); digitkeypad_c.setOnClickListener(dkol); digitkeypad_ok.setOnClickListener(new DigitPasswordKeypadFinshOnClikcListener()); return v; } private class DigitPasswordKeypadFinshOnClikcListener implements OnClickListener { @Override public void onClick(View v) { int viewId = v.getId(); if (viewId == R.id.digitkeypad_ok) { // 点击完成 // 设置值回页面 // 隐藏自己View // if (isPwd) { // jsimpl.hidePasswdPad(digitkeypad_edittext.getText().toString()); // } else { // jsimpl.hideCallNumPad(digitkeypad_edittext.getText().toString()); // } DigitPasswordKeyPad.this.setVisibility(View.GONE); } } } public void initInputLable(String str, int length) { str = str.trim(); digitnum = str; this.length = length; digitkeypad_edittext.setText(digitnum); digitkeypad_edittext.setSelection(digitnum.length()); } private class DigitPasswordKeypadOnClickListener implements OnClickListener { @Override public void onClick(View v) { int viewId = v.getId(); switch (viewId) { case R.id.digitkeypad_1: if (digitnum.length() == length) { return; } else { digitnum += 1; } break; case R.id.digitkeypad_2: if (digitnum.length() == length) { return; } else { digitnum += 2; } break; case R.id.digitkeypad_3: if (digitnum.length() == length) { return; } else { digitnum += 3; } break; case R.id.digitkeypad_4: if (digitnum.length() == length) { return; } else { digitnum += 4; } break; case R.id.digitkeypad_5: if (digitnum.length() == length) { return; } else { digitnum += 5; } break; case R.id.digitkeypad_6: if (digitnum.length() == length) { return; } else { digitnum += 6; } break; case R.id.digitkeypad_7: if (digitnum.length() == length) { return; } else { digitnum += 7; } break; case R.id.digitkeypad_8: if (digitnum.length() == length) { return; } else { digitnum += 8; } break; case R.id.digitkeypad_9: if (digitnum.length() == length) { return; } else { digitnum += 9; } break; case R.id.digitkeypad_0: if (digitnum.length() == length) { return; } else { digitnum += 0; } break; case R.id.digitkeypad_c:// 后退 if (digitnum.length() > 0) { digitnum = digitnum.substring(0, digitnum.length() - 1); } break; } // 格式化 数据 digitkeypad_edittext.setText(digitnum); digitkeypad_edittext.setSelection(null != digitnum ? digitnum.length() : 0); } } }