Chinaunix首页 | 论坛 | 博客
  • 博客访问: 353452
  • 博文数量: 107
  • 博客积分: 927
  • 博客等级: 大尉
  • 技术积分: 865
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-13 17:50
文章分类

全部博文(107)

文章存档

2014年(2)

2013年(13)

2012年(16)

2011年(76)

分类: Android平台

2013-03-15 17:10:00

先上图

先看一下键盘的布局文件control_digitpasswordkeypad.xml
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/digitkeypadrootlayout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.    
  8.     <LinearLayout  
  9.         android:id="@+id/transpwdpdpanel"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_weight="1"  
  13.         android:background="#000000" >  
  14.     LinearLayout>  
  15.    
  16.     <LinearLayout  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_weight="0"  
  20.         android:background="@drawable/keyboardbackground"  
  21.         android:gravity="bottom"  
  22.         android:orientation="vertical"  
  23.         android:paddingBottom="4dp"  
  24.         android:paddingLeft="4dp"  
  25.         android:paddingRight="4dp" >  
  26.    
  27.         <LinearLayout  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_marginLeft="4dp"  
  31.             android:layout_marginRight="4dp"  
  32.             android:orientation="horizontal" >  
  33.    
  34.             <EditText  
  35.                 android:id="@+id/digitpadedittext"  
  36.                 android:layout_width="fill_parent"  
  37.                 android:layout_height="wrap_content"  
  38.                 android:gravity="center_vertical|right"  
  39.                 android:singleLine="true" />  
  40.         LinearLayout>  
  41.    
  42.         <LinearLayout  
  43.             android:layout_width="fill_parent"  
  44.             android:layout_height="wrap_content"  
  45.             android:orientation="horizontal" >  
  46.    
  47.             <Button  
  48.                 android:id="@+id/digitkeypad_1"  
  49.                 android:layout_width="wrap_content"  
  50.                 android:layout_height="46dp"  
  51.                 android:layout_marginLeft="4dp"  
  52.                 android:layout_marginRight="2dp"  
  53.                 android:layout_weight="1"  
  54.                 android:background="@drawable/widget_keypad_digit"  
  55.                 android:gravity="center"  
  56.                 android:text="1"  
  57.                 android:textSize="22sp" >  
  58.             Button>  
  59.    
  60.             <Button  
  61.                 android:id="@+id/digitkeypad_2"  
  62.                 android:layout_width="wrap_content"  
  63.                 android:layout_height="46dp"  
  64.                 android:layout_marginLeft="2dp"  
  65.                 android:layout_marginRight="3dp"  
  66.                 android:layout_weight="1"  
  67.                 android:background="@drawable/widget_keypad_digit"  
  68.                 android:gravity="center"  
  69.                 android:text="2"  
  70.                 android:textSize="22sp" >  
  71.             Button>  
  72.    
  73.             <Button  
  74.                 android:id="@+id/digitkeypad_3"  
  75.                 android:layout_width="wrap_content"  
  76.                 android:layout_height="46dp"  
  77.                 android:layout_marginLeft="2dp"  
  78.                 android:layout_marginRight="4dp"  
  79.                 android:layout_weight="1"  
  80.                 android:background="@drawable/widget_keypad_digit"  
  81.                 android:gravity="center"  
  82.                 android:text="3"  
  83.                 android:textSize="22sp" >  
  84.             Button>  
  85.         LinearLayout>  
  86.    
  87.         <LinearLayout  
  88.             android:layout_width="fill_parent"  
  89.             android:layout_height="wrap_content"  
  90.             android:orientation="horizontal" >  
  91.    
  92.             <Button  
  93.                 android:id="@+id/digitkeypad_4"  
  94.                 android:layout_width="wrap_content"  
  95.                 android:layout_height="46dp"  
  96.                 android:layout_marginLeft="4dp"  
  97.                 android:layout_marginRight="2dp"  
  98.                 android:layout_weight="1"  
  99.                 android:background="@drawable/widget_keypad_digit"  
  100.                 android:text="4"  
  101.                 android:textSize="22sp" >  
  102.             Button>  
  103.    
  104.             <Button  
  105.                 android:id="@+id/digitkeypad_5"  
  106.                 android:layout_width="wrap_content"  
  107.                 android:layout_height="46dp"  
  108.                 android:layout_marginLeft="2dp"  
  109.                 android:layout_marginRight="3dp"  
  110.                 android:layout_weight="1"  
  111.                 android:background="@drawable/widget_keypad_digit"  
  112.                 android:text="5"  
  113.                 android:textSize="22sp" >  
  114.             Button>  
  115.    
  116.             <Button  
  117.                 android:id="@+id/digitkeypad_6"  
  118.                 android:layout_width="wrap_content"  
  119.                 android:layout_height="46dp"  
  120.                 android:layout_marginLeft="2dp"  
  121.                 android:layout_marginRight="4dp"  
  122.                 android:layout_weight="1"  
  123.                 android:background="@drawable/widget_keypad_digit"  
  124.                 android:text="6"  
  125.                 android:textSize="22sp" >  
  126.             Button>  
  127.         LinearLayout>  
  128.    
  129.         <LinearLayout  
  130.             android:layout_width="fill_parent"  
  131.             android:layout_height="wrap_content"  
  132.             android:orientation="horizontal" >  
  133.    
  134.             <Button  
  135.                 android:id="@+id/digitkeypad_7"  
  136.                 android:layout_width="80dp"  
  137.                 android:layout_height="46dp"  
  138.                 android:layout_marginLeft="4dp"  
  139.                 android:layout_marginRight="2dp"  
  140.                 android:layout_weight="1"  
  141.                 android:background="@drawable/widget_keypad_digit"  
  142.                 android:text="7"  
  143.                 android:textSize="22sp" >  
  144.             Button>  
  145.    
  146.             <Button  
  147.                 android:id="@+id/digitkeypad_8"  
  148.                 android:layout_width="80dp"  
  149.                 android:layout_height="46dp"  
  150.                 android:layout_marginLeft="2dp"  
  151.                 android:layout_marginRight="3dp"  
  152.                 android:layout_weight="1"  
  153.                 android:background="@drawable/widget_keypad_digit"  
  154.                 android:text="8"  
  155.                 android:textSize="22sp" >  
  156.             Button>  
  157.    
  158.             <Button  
  159.                 android:id="@+id/digitkeypad_9"  
  160.                 android:layout_width="80dp"  
  161.                 android:layout_height="46dp"  
  162.                 android:layout_marginLeft="2dp"  
  163.                 android:layout_marginRight="4dp"  
  164.                 android:layout_weight="1"  
  165.                 android:background="@drawable/widget_keypad_digit"  
  166.                 android:text="9"  
  167.                 android:textSize="22sp" >  
  168.             Button>  
  169.         LinearLayout>  
  170.    
  171.         <LinearLayout  
  172.             android:layout_width="fill_parent"  
  173.             android:layout_height="wrap_content"  
  174.             android:orientation="horizontal" >  
  175.    
  176.             <Button  
  177.                 android:id="@+id/digitkeypad_c"  
  178.                 android:layout_width="80dp"  
  179.                 android:layout_height="46dp"  
  180.                 android:layout_marginLeft="4dp"  
  181.                 android:layout_marginRight="2dp"  
  182.                 android:layout_weight="1"  
  183.                 android:background="@drawable/keyboardclear"  
  184.                 android:text=""  
  185.                 android:textSize="22sp" >  
  186.             Button>  
  187.    
  188.             <Button  
  189.                 android:id="@+id/digitkeypad_0"  
  190.                 android:layout_width="80dp"  
  191.                 android:layout_height="46dp"  
  192.                 android:layout_marginLeft="2dp"  
  193.                 android:layout_marginRight="3dp"  
  194.                 android:layout_weight="1"  
  195.                 android:background="@drawable/widget_keypad_digit"  
  196.                 android:text="0"  
  197.                 android:textSize="22sp" >  
  198.             Button>  
  199.    
  200.             <Button  
  201.                 android:id="@+id/digitkeypad_ok"  
  202.                 android:layout_width="80dp"  
  203.                 android:layout_height="46dp"  
  204.                 android:layout_marginLeft="2dp"  
  205.                 android:layout_marginRight="4dp"  
  206.                 android:layout_weight="1"  
  207.                 android:background="@drawable/keyboardok"  
  208.                 android:text=""  
  209.                 android:textSize="22sp" >  
  210.             Button>  
  211.         LinearLayout>  
  212.     LinearLayout>  
  213.    
  214. LinearLayout>  

键盘布局文件截图如下:

下面看一下主界面的布局文件mian.xml
  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.    
  7.     <TextView  
  8.         android:id="@+id/input"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:hint="请输入数字"  
  12.         />  
  13.    
  14. LinearLayout>  

主界面只有一个TextView控件,通过触发TextView的单击事件弹出自定义键盘

触发事件代码:

  1. TextView editText = (TextView)findViewById(R.id.input);  
  2. editText.setOnClickListener(new OnClickListener() {  
  3.     @Override  
  4.     public void onClick(View v) {  
  5.         showPassWdPadView();  
  6.     }  
  7. });  

弹出自定义键盘方法showPassWdPadView()
  1. private void showPassWdPadView() {  
  2.         this.runOnUiThread(new Runnable() {  
  3.             public void run() {  
  4.                 // 让一个视图浮动在你的应用程序之上  
  5.                 WindowManager windowmanager = (WindowManager) content.getSystemService(Context.WINDOW_SERVICE);  
  6.                 LayoutParams layoutparams = new LayoutParams(-1, -1, WindowManager.LayoutParams.FIRST_SUB_WINDOW, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888);  
  7.                 layoutparams.gravity = Gravity.BOTTOM;  
  8.                 passwdview.findViewById(R.id.transpwdpdpanel).getBackground().setAlpha(140);  
  9.                 windowmanager.addView(passwdview, layoutparams);  
  10.             }  
  11.         });  
  12. }  

这里用到了WindowManager,具体用法可参考网上。

下面是主界面的全部代码

  1. import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.graphics.PixelFormat;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.View;  
  7. import android.view.WindowManager;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.WindowManager.LayoutParams;  
  10. import android.widget.TextView;  
  11.    
  12. public class AndroidInputNumberActivity extends Activity {  
  13.     private DigitPasswordKeyPad dpk;  
  14.     private View passwdview;  
  15.     private Context content ;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.           
  21.         content = this;  
  22.           
  23.         dpk = new DigitPasswordKeyPad(this);  
  24.         passwdview = dpk.setup();  
  25.           
  26.         TextView editText = (TextView)findViewById(R.id.input);  
  27.         editText.setOnClickListener(new OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 showPassWdPadView();  
  31.             }  
  32.         });  
  33.     }  
  34.       
  35.     private void showPassWdPadView() {  
  36.             this.runOnUiThread(new Runnable() {  
  37.                 public void run() {  
  38.                     // 让一个视图浮动在你的应用程序之上  
  39.                     WindowManager windowmanager = (WindowManager) content.getSystemService(Context.WINDOW_SERVICE);  
  40.                     LayoutParams layoutparams = new LayoutParams(-1, -1, WindowManager.LayoutParams.FIRST_SUB_WINDOW, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888);  
  41.                     layoutparams.gravity = Gravity.BOTTOM;  
  42.                     passwdview.findViewById(R.id.transpwdpdpanel).getBackground().setAlpha(140);  
  43.                     windowmanager.addView(passwdview, layoutparams);  
  44.                 }  
  45.             });  
  46.     }  
  47. }  

上面代码中passwdview这个对象就是弹出键盘界面的View实例,passwdview这个对象是在DigitPasswordKeyPad中初始化得到。
DigitPasswordKeyPad类自定义键盘类,继承自View类
  1. import android.content.Context;  
  2. import android.text.InputType;  
  3. import android.view.LayoutInflater;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.widget.EditText;  
  7.    
  8. /** 
  9.  * 系统定制的数字密码键盘 
  10.  * 
  11.  * @author twf 
  12.  * 
  13.  */  
  14. public class DigitPasswordKeyPad extends View {  
  15.     private Context ctx = null;  
  16.     private View v;  
  17.     private String digitnum = "";  
  18.     private int length = 6;  
  19.    
  20.     private Button digitkeypad_1;  
  21.     private Button digitkeypad_2;  
  22.     private Button digitkeypad_3;  
  23.     private Button digitkeypad_4;  
  24.     private Button digitkeypad_5;  
  25.     private Button digitkeypad_6;  
  26.     private Button digitkeypad_7;  
  27.     private Button digitkeypad_8;  
  28.     private Button digitkeypad_9;  
  29.     private Button digitkeypad_0;  
  30.     private Button digitkeypad_c;  
  31.     private Button digitkeypad_ok;  
  32.     private EditText digitkeypad_edittext;  
  33.    
  34.     private boolean isPwd;  
  35.    
  36.     public DigitPasswordKeyPad(Context ctx) {  
  37.         super(ctx);  
  38.         this.ctx = ctx;  
  39.     }  
  40.    
  41.     @Override  
  42.     protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {  
  43.     }  
  44.    
  45.     public void setEditTextIsPwd(boolean ispwd) {  
  46.         if (ispwd) {  
  47.             digitkeypad_edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);  
  48.         } else {  
  49.             digitkeypad_edittext.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);  
  50.         }  
  51.         this.isPwd = ispwd;  
  52.     }  
  53.    
  54.     public View setup() {  
  55.         LayoutInflater lif = LayoutInflater.from(ctx);  
  56.         v = lif.inflate(R.layout.control_digitpasswordkeypad, null);  
  57.    
  58.         // 初始化 对象  
  59.         digitkeypad_1 = (Button) v.findViewById(R.id.digitkeypad_1);  
  60.         digitkeypad_2 = (Button) v.findViewById(R.id.digitkeypad_2);  
  61.         digitkeypad_3 = (Button) v.findViewById(R.id.digitkeypad_3);  
  62.         digitkeypad_4 = (Button) v.findViewById(R.id.digitkeypad_4);  
  63.         digitkeypad_5 = (Button) v.findViewById(R.id.digitkeypad_5);  
  64.         digitkeypad_6 = (Button) v.findViewById(R.id.digitkeypad_6);  
  65.         digitkeypad_7 = (Button) v.findViewById(R.id.digitkeypad_7);  
  66.         digitkeypad_8 = (Button) v.findViewById(R.id.digitkeypad_8);  
  67.         digitkeypad_9 = (Button) v.findViewById(R.id.digitkeypad_9);  
  68.         digitkeypad_0 = (Button) v.findViewById(R.id.digitkeypad_0);  
  69.         digitkeypad_c = (Button) v.findViewById(R.id.digitkeypad_c);  
  70.         digitkeypad_ok = (Button) v.findViewById(R.id.digitkeypad_ok);  
  71.         digitkeypad_edittext = (EditText) v.findViewById(R.id.digitpadedittext);  
  72.    
  73.         // 添加点击事件  
  74.         DigitPasswordKeypadOnClickListener dkol = new DigitPasswordKeypadOnClickListener();  
  75.         digitkeypad_1.setOnClickListener(dkol);  
  76.         digitkeypad_2.setOnClickListener(dkol);  
  77.         digitkeypad_3.setOnClickListener(dkol);  
  78.         digitkeypad_4.setOnClickListener(dkol);  
  79.         digitkeypad_5.setOnClickListener(dkol);  
  80.         digitkeypad_6.setOnClickListener(dkol);  
  81.         digitkeypad_7.setOnClickListener(dkol);  
  82.         digitkeypad_8.setOnClickListener(dkol);  
  83.         digitkeypad_9.setOnClickListener(dkol);  
  84.         digitkeypad_0.setOnClickListener(dkol);  
  85.         digitkeypad_c.setOnClickListener(dkol);  
  86.         digitkeypad_ok.setOnClickListener(new DigitPasswordKeypadFinshOnClikcListener());  
  87.    
  88.         return v;  
  89.     }  
  90.    
  91.     private class DigitPasswordKeypadFinshOnClikcListener implements OnClickListener {  
  92.    
  93.         @Override  
  94.         public void onClick(View v) {  
  95.             int viewId = v.getId();  
  96.    
  97.             if (viewId == R.id.digitkeypad_ok) {  
  98.                 // 点击完成  
  99.                 // 设置值回页面  
  100.                 // 隐藏自己View  
  101.                 // if (isPwd) {  
  102.                 // jsimpl.hidePasswdPad(digitkeypad_edittext.getText().toString());  
  103.                 // } else {  
  104.                 // jsimpl.hideCallNumPad(digitkeypad_edittext.getText().toString());  
  105.                 // }  
  106.                 DigitPasswordKeyPad.this.setVisibility(View.GONE);  
  107.             }  
  108.         }  
  109.     }  
  110.    
  111.     public void initInputLable(String str, int length) {  
  112.         str = str.trim();  
  113.         digitnum = str;  
  114.         this.length = length;  
  115.         digitkeypad_edittext.setText(digitnum);  
  116.         digitkeypad_edittext.setSelection(digitnum.length());  
  117.     }  
  118.    
  119.     private class DigitPasswordKeypadOnClickListener implements OnClickListener {  
  120.    
  121.         @Override  
  122.         public void onClick(View v) {  
  123.             int viewId = v.getId();  
  124.    
  125.             switch (viewId) {  
  126.             case R.id.digitkeypad_1:  
  127.                 if (digitnum.length() == length) {  
  128.                     return;  
  129.                 } else {  
  130.                     digitnum += 1;  
  131.                 }  
  132.                 break;  
  133.             case R.id.digitkeypad_2:  
  134.                 if (digitnum.length() == length) {  
  135.                     return;  
  136.                 } else {  
  137.                     digitnum += 2;  
  138.                 }  
  139.                 break;  
  140.             case R.id.digitkeypad_3:  
  141.                 if (digitnum.length() == length) {  
  142.                     return;  
  143.                 } else {  
  144.                     digitnum += 3;  
  145.                 }  
  146.                 break;  
  147.             case R.id.digitkeypad_4:  
  148.                 if (digitnum.length() == length) {  
  149.                     return;  
  150.                 } else {  
  151.                     digitnum += 4;  
  152.                 }  
  153.                 break;  
  154.             case R.id.digitkeypad_5:  
  155.                 if (digitnum.length() == length) {  
  156.                     return;  
  157.                 } else {  
  158.                     digitnum += 5;  
  159.                 }  
  160.                 break;  
  161.             case R.id.digitkeypad_6:  
  162.                 if (digitnum.length() == length) {  
  163.                     return;  
  164.                 } else {  
  165.                     digitnum += 6;  
  166.                 }  
  167.                 break;  
  168.             case R.id.digitkeypad_7:  
  169.                 if (digitnum.length() == length) {  
  170.                     return;  
  171.                 } else {  
  172.                     digitnum += 7;  
  173.                 }  
  174.                 break;  
  175.             case R.id.digitkeypad_8:  
  176.                 if (digitnum.length() == length) {  
  177.                     return;  
  178.                 } else {  
  179.                     digitnum += 8;  
  180.                 }  
  181.                 break;  
  182.             case R.id.digitkeypad_9:  
  183.                 if (digitnum.length() == length) {  
  184.                     return;  
  185.                 } else {  
  186.                     digitnum += 9;  
  187.                 }  
  188.                 break;  
  189.             case R.id.digitkeypad_0:  
  190.                 if (digitnum.length() == length) {  
  191.                     return;  
  192.                 } else {  
  193.                     digitnum += 0;  
  194.                 }  
  195.    
  196.                 break;  
  197.             case R.id.digitkeypad_c:// 后退  
  198.                 if (digitnum.length() > 0) {  
  199.                     digitnum = digitnum.substring(0, digitnum.length() - 1);  
  200.                 }  
  201.                 break;  
  202.             }  
  203.             // 格式化 数据  
  204.             digitkeypad_edittext.setText(digitnum);  
  205.             digitkeypad_edittext.setSelection(null != digitnum ? digitnum.length() : 0);  
  206.         }  
  207.    
  208.     }  


阅读(2226) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~