先上图
先看一下键盘的布局文件control_digitpasswordkeypad.xml
键盘布局文件截图如下:
下面看一下主界面的布局文件mian.xml
-
xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns: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();
-
}
-
});
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);
-
}
-
});
-
}
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);
-
}
-
});
-
}
-
}
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类
阅读(3946) | 评论(0) | 转发(0) |