素材巴巴 > 程序开发 >

【android开发】自定义数字软键盘的设计与实现(2)

程序开发 2023-09-12 09:14:41

    上一篇我们说了关于自定义软键盘实现的相关原理,把两个主要的类介绍了一下,并看了一下源码的内容,那么今天实现起来就不会有什么疑惑了,每一步都会清晰了。


       好了,下面我们就把实现的具体步骤给大家介绍一下:首先在res下新建xml文件夹,在xml文件夹中新建symbols.xml文件,这个布局文件重要是实现软键盘的布局,每一个按键都有一个codes值,在类中就是通过codes值来监听每一个按钮,上一面已经说了,一些codes是固定,一些是自定义设置的,内如如下:

  
                                              
   


然后创建一个类,用于处理软键盘事件,文件名为KeyboardUtil.java,内容如下:

package com.xinhui.ui;import com.xinhui.appsystem.R;import android.app.Activity;  
 import android.content.Context;  
 import android.inputmethodservice.Keyboard;  
 import android.inputmethodservice.KeyboardView;  
 import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;  
 import android.text.Editable;  
 import android.view.View;  
 import android.widget.EditText;  public class KeyboardUtil {  private KeyboardView keyboardView;  private Keyboard k;// 数字键盘  private EditText ed;public KeyboardUtil(Activity act, Context ctx, EditText edit) {  this.ed = edit;k = new Keyboard(ctx, R.xml.symbols);  keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);  keyboardView.setKeyboard(k);  keyboardView.setEnabled(true);  keyboardView.setPreviewEnabled(true);keyboardView.setVisibility(View.VISIBLE);keyboardView.setOnKeyboardActionListener(listener);  }  private OnKeyboardActionListener listener = new OnKeyboardActionListener() {  @Override  public void swipeUp() {  }  @Override  public void swipeRight() {  }  @Override  public void swipeLeft() {  }  @Override  public void swipeDown() {  }  @Override  public void onText(CharSequence text) {  }  @Override  public void onRelease(int primaryCode) {  }  @Override  public void onPress(int primaryCode) {  }  //一些特殊操作按键的codes是固定的比如完成、回退等@Override  public void onKey(int primaryCode, int[] keyCodes) {  Editable editable = ed.getText();  int start = ed.getSelectionStart();  if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退  if (editable != null && editable.length() > 0) {  if (start > 0) {  editable.delete(start - 1, start);  }  }  }else if (primaryCode == 4896) {// 清空 editable.clear();} else { //将要输入的数字现在编辑框中 editable.insert(start, Character.toString((char) primaryCode));  }  }  };  public void showKeyboard() {  int visibility = keyboardView.getVisibility();  if (visibility == View.GONE || visibility == View.INVISIBLE) {  keyboardView.setVisibility(View.VISIBLE);  }  }  
 } 

接下来就是实现activity的视图布局文件了,文件名为input_pwd.xml,内容如下:


              
 
最后就在你要执行activity,添加一些代码就行了,剩下的就和其他控件使用方式一样了,类名为LockAppActivity.java,内容如下:

package com.xinhui.ui;import com.xinhui.appsystem.R;
 import com.xinhui.service.WatchAppService;import android.app.Activity;
 import android.content.SharedPreferences;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.text.InputType;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.View.OnTouchListener;
 import android.view.Window;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;
 /*** 类名称:LockAppActivity * 类描述:系统锁页面* 创建人:LXH * 创建时间:2013-10-21 上午10:30:00 */
 public class LockAppActivity extends Activity implements OnClickListener,OnTouchListener{private ImageView ivLockAppIcon;private TextView tvLockAppName;private EditText etInputPwd;private Button btnConfirm;private String packageName;private String passWord;private SharedPreferences preferences;//public static boolean isLock;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.input_pwd);//WatchAppService.isLock = false;Log.e("test", "onCreate--->");ivLockAppIcon = (ImageView) findViewById(R.id.iv_lock_app_icon);  tvLockAppName = (TextView) findViewById(R.id.tv_lock_app_name);  etInputPwd = (EditText) findViewById(R.id.et_lock_pwd);etInputPwd.setOnTouchListener(this);btnConfirm = (Button) findViewById(R.id.btn_confirm);btnConfirm.setOnClickListener(this);packageName = getIntent().getStringExtra("packageName");try {//通过包名拿到applicationInfoApplicationInfo appInfo = getPackageManager().getPackageInfo(packageName, 0).applicationInfo;//应用图标  Drawable app_icon = appInfo.loadIcon(getPackageManager());  //应用的名字  String app_name = appInfo.loadLabel(getPackageManager()).toString();ivLockAppIcon.setImageDrawable(app_icon);  tvLockAppName.setText(app_name);  } catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//不让用户按后退键  @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stub//屏蔽后退键  if(KeyEvent.KEYCODE_BACK == event.getKeyCode())  {  return true;//阻止事件继续向下分发  }  return super.onKeyDown(keyCode, event); }@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString input = etInputPwd.getText().toString().trim();preferences = getSharedPreferences("passWord", MODE_PRIVATE);passWord = preferences.getString("pwd", "");if(TextUtils.isEmpty(input))  {  Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();  }  else if(passWord.equals(input))  { WatchAppService.lastRunningApp = WatchAppService.runningApp;//这里赋值,终于解决了反复弹出验证页面的BUGfinish();  }  else  {  Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();etInputPwd.setText("");//置空}  }@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stub//这样是在触摸到控件时,软键盘才会显示出来int inputback = etInputPwd.getInputType();  etInputPwd.setInputType(InputType.TYPE_NULL);  new KeyboardUtil(this, this, etInputPwd).showKeyboard();  etInputPwd.setInputType(inputback); return false;}}
 

由于我的这个activity是其他activity调用的,并不是主界面的activity。上面就把实现的整个过程写完了,一个自定义的数字软键盘就实现了。


源码下载http://download.csdn.net/detail/lixinhuixin/6462065


标签:

上一篇: git高级用法 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。