Android自定義密碼鍵盤
先上圖
先看一下鍵盤的布局文件control_digitpasswordkeypad.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:andro
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:
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);
}
}
}
最後更新:2017-04-02 16:47:54