android-使用PopupWindow实现随机排列的自定义密码键盘(1)

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"?>

<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” />

keyboard_bg_small.xml

<?xml version="1.0" encoding="utf-8"?>

<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” />

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” />

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” />

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”

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
t_width=“fill_parent”

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-vNvzzHcG-1715375430958)]

[外链图片转存中…(img-gzPMI313-1715375430961)]

[外链图片转存中…(img-vu2yh4eH-1715375430962)]

[外链图片转存中…(img-KPIBeDLo-1715375430963)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值