亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

android-使用PopupWindow實現隨機排列的自定義

系統 2290 0

效果圖:

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實現隨機排列的自定義密碼鍵盤


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久人人爽人人爽人人片av不 | 国产色站| 日本精品夜色视频一区二区 | 一a一级片| 久久不见久久见免费影院 | 一级成人毛片免费观看欧美 | 天天摸天天碰色综合网 | 久久精品综合国产二区 | 真实国产乱人伦在线视频播放 | 亚洲人成一区 | 99热这里只有成人精品国产 | 亚洲精品中文字幕第一区 | 一级毛片在线观看视频 | 亚洲国产精久久久久久久 | 国产欧美久久久精品 | 国产女人伦码一区二区三区不卡 | 亚洲美女激情视频 | 伊人热 | 日韩中文字幕视频在线观看 | 按摩一级片 | 亚洲视频一区在线观看 | 中国美女一级a毛片录像在线 | 欧美在线一区二区三区 | 老妇女人一级毛片 | 亚洲一区二区天海翼 | 亚洲精品一二三 | 国产欧美综合在线一区二区三区 | 国产精品国产亚洲精品不卡 | 宅男影院在线观看 | 另类亚洲视频 | 牛牛影视在线入口 | 综合视频网 | 亚洲美女视频在线观看 | 狠狠狠狠狠狠干 | 天天做天天做天天综合网 | 亚洲一区二区精品推荐 | 看一级毛片一区二区三区免费 | 免费男女网站 | 国产一区二区三区精品视频 | 四虎福利 | 成人国产片免费 |