- 浏览: 103565 次
文章分类
最新评论
-
blocker205:
哥们,可以发个demo给我吗?万分感谢。1358687240@ ...
android 仿QQ聊天,带表情,可翻页,带翻页拖动缓冲 -
LD_21:
程序员妻子自述: 那些程序员教给我的 -
alafqq:
看完一定要顶啊
程序员妻子自述: 那些程序员教给我的 -
iceternally:
~~不错~~
程序员妻子自述: 那些程序员教给我的 -
it2008chen:
不得不顶,幽默诙谐的语气。口才不错!靠,今年也没给我涨工资。弱 ...
应该怎么管理程序员?
android-使用PopupWindow实现随机排列的自定义密码键盘
效果图:
MainActivity.java
View keyboardView = LayoutInflater.from(this).inflate( R.layout.random_keyboard, null); Display display = getWindowManager().getDefaultDisplay(); int height = (int) getResources().getDimension(R.dimen.height); popupWindow = new PopupWindow(keyboardView, display.getWidth(), height * 4, false); btn0 = (Button) keyboardView.findViewById(R.id.keyboard_btn0); btn1 = (Button) keyboardView.findViewById(R.id.keyboard_btn1); btn2 = (Button) keyboardView.findViewById(R.id.keyboard_btn2); btn3 = (Button) keyboardView.findViewById(R.id.keyboard_btn3); btn4 = (Button) keyboardView.findViewById(R.id.keyboard_btn4); btn5 = (Button) keyboardView.findViewById(R.id.keyboard_btn5); btn6 = (Button) keyboardView.findViewById(R.id.keyboard_btn6); btn7 = (Button) keyboardView.findViewById(R.id.keyboard_btn7); btn8 = (Button) keyboardView.findViewById(R.id.keyboard_btn8); btn9 = (Button) keyboardView.findViewById(R.id.keyboard_btn9); btn_del = (Button) keyboardView.findViewById(R.id.keyboard_btn_del); btn_clear = (Button) keyboardView.findViewById(R.id.keyboard_btn_clear); btn_conf = (Button) keyboardView.findViewById(R.id.keyboard_btn_conf); btn0.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[0]+""; editable.insert(index, str); } }); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[1]+""; editable.insert(index, str); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[2]+""; editable.insert(index, str); } }); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[3]+""; editable.insert(index, str); } }); btn4.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[4]+""; editable.insert(index, str); } }); btn5.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[5]+""; editable.insert(index, str); } }); btn6.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[6]+""; editable.insert(index, str); } }); btn7.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[7]+""; editable.insert(index, str); } }); btn8.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[8]+""; editable.insert(index, str); } }); btn9.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub int index = editText.getSelectionStart(); String str = randomKeys[9]+""; editable.insert(index, str); } }); btn_del.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { int index = editText.getSelectionStart(); editable.delete(index - 1, index); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btn_clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub editable.clear(); } }); btn_conf.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub popupWindow.dismiss(); button.requestFocus(); } }); // 禁止EditText获得焦点后弹出系统键盘 editText.setInputType(InputType.TYPE_NULL); editText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean arg1) { // TODO Auto-generated method stub if (arg1) { updateKeyBoard(); popupWindow.showAsDropDown(editText, 0, 0); popupWindow.update(); } } }); } private void updateKeyBoard() { randomKeys = getRandomNum(); btn0.setText(randomKeys[0] + ""); btn1.setText(randomKeys[1] + ""); btn2.setText(randomKeys[2] + ""); btn3.setText(randomKeys[3] + ""); btn4.setText(randomKeys[4] + ""); btn5.setText(randomKeys[5] + ""); btn6.setText(randomKeys[6] + ""); btn7.setText(randomKeys[7] + ""); btn8.setText(randomKeys[8] + ""); btn9.setText(randomKeys[9] + ""); } //生成随机的0-9 10个数字,且值各不相同 private int[] getRandomNum() { Random random = new Random(); int[] data = new int[10]; boolean b; boolean b2 = false; boolean b3 = true; int x; for (int i = 0; i < 10; i++) { b = true; while (b) { x = random.nextInt(10); if (x == 0 && b3) { b3 = false; b = false; } for (int y : data) { if (x == y) { b2 = false; break; } else { b2 = true; } } if (b2) { data[i] = x; b = false; break; } } } return data; }
keyboard_bg_big.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/key11"/> <item android:state_focused="true" android:drawable="@drawable/key12"/> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/keyboard_key_style" /> </selector>
keyboard_bg_small.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/key11"/> <item android:state_focused="true" android:drawable="@drawable/key12"/> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/keyboard_key_style" /> </selector>
keyboard_bg_style.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="90.0" android:endColor="#ffffff" android:startColor="#ffffff" /> <corners android:bottomLeftRadius="6.0dip" android:bottomRightRadius="6.0dip" android:topLeftRadius="6.0dip" android:topRightRadius="6.0dip" /> </shape>
keyboard_key_style.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <size android:height="36.0dip" android:width="80.0dip" /> <gradient android:angle="90.0" android:endColor="#D7D7D7" android:startColor="#D7D7D7" /> <corners android:bottomLeftRadius="6.0dip" android:bottomRightRadius="6.0dip" android:topLeftRadius="6.0dip" android:topRightRadius="6.0dip" /> </shape>
random_keyboard.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/keyboard_bg_style" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="3dp" android:orientation="horizontal" > <Button android:id="@+id/keyboard_btn1" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="1" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn2" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="2" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn3" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="3" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn9" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="." android:textSize="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="3dp" android:orientation="horizontal" > <Button android:id="@+id/keyboard_btn4" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="4" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn5" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="5" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn6" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="6" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn0" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="0" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="3dp" android:orientation="horizontal" > <Button android:id="@+id/keyboard_btn7" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_weight="3" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="7" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn8" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="3" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="8" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn_del" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="2" android:background="@drawable/keyboard_bg_small" android:gravity="center_horizontal|center_vertical" android:text="删除" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="3dp" android:orientation="horizontal" > <Button android:id="@+id/keyboard_btn_clear" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_big" android:gravity="center_horizontal|center_vertical" android:text="清空" android:textSize="20dp" /> <Button android:id="@+id/keyboard_btn_conf" android:layout_width="fill_parent" android:layout_height="55dp" android:layout_marginLeft="3dp" android:layout_weight="1" android:background="@drawable/keyboard_bg_big" android:gravity="center_horizontal|center_vertical" android:text="确定" android:textSize="20dp" /> </LinearLayout> </LinearLayout>
源码下载:http://download.csdn.net/detail/centralperk/5075724
相关推荐
android-使用PopupWindow实现随机排列的自定义密码键盘
使用PopupWindow实现的Popup菜单,PopupMenu可以根据ListView item的位置变化,源码比较简单,是学习PopupWindow的好材料。项目地址:https://github.com/u1aryz/Android-NewPopupMenu 效果图:如何使用创建菜单实例...
Android--开发--PopupWindow下拉列表
android通过使用popupwindow控件实现自定义menu菜单
Android自定义下拉框,用PopupWindow实现
使用PopupWindow实现下拉列表的选择.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android 封装一个通用PopupWindow,避免重复造轮子,简化代码,方便管理。
android使用PopUpWindow自定义下拉框
Android开发之PopupWindow实现弹窗
PopupWindowSet 安卓PopupWindow组件的一般使用方式,各种自定义的效果,以及位置的设定 运行环境为:eclipse-adt
安卓Android源码——Android之用PopupWindow实现弹出菜单.zip
安卓app开发项目-PopupWindow下拉列表(源码).zip安卓app开发项目-PopupWindow下拉列表(源码).zip安卓app开发项目-PopupWindow下拉列表(源码).zip安卓app开发项目-PopupWindow下拉列表(源码).zip安卓app开发项目-...
在使用UC-WebBrowser时,你会发现它的弹出菜单跟系统自带的菜单不一样。它实现更多菜单选项的显示和分栏。其实,它的本身是PopupWindow或者是AlertDialog对话框,在里面添加两个GridView控件,一个是菜单标题栏,一...
AndroidListView+PopupWindow实现item点击弹出PopupWindow 博客地址:https://blog.csdn.net/dreams_deng/article/details/104618270
安卓开发-Android之用PopupWindow实现弹出菜单.zip
android自定义PopupWindow,实现类似微信弹框类别选项。
(2)Popupwindow默认不会响应物理键盘的back,除非显示设置了popup.setFocusable(true);而在点击back的时候,Dialog会消失。 (3)Popupwindow不会给页面其他的部分添加蒙层,而Dialog会。 (4)Popupwindow没有...
AS写的PopupWindow demo blog:http://blog.csdn.net/shenyuanqing/article/details/48136639
Android源码——PopupWindow实现弹出菜单.zip
本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法。分享给大家供大家参考,具体如下: 布局文件: 主布局文件:activity_main: <?xml version=1.0 encoding=utf-8?> <...