直接上代码
EtZdyActivity:

package com.example.softkeyboardandroidtest;


import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

public class EtZdyActivity extends Activity{ 
	String SDCARD_DIR = Environment.getExternalStorageDirectory().getPath();
	String dbfile = SDCARD_DIR + "/Download/main.db";
	
	private EditText et;
	
	private TextView tv;
	private Button btnKeyboard1;
	private Button btnKeyboard2;
	private Button btnKeyboard3;
	private Button btnKeyboard4;
	private Button btnKeyboard5;
	private Button btnKeyboard6;
	private Button btnKeyboard7;
	private Button btnKeyboard8;
	private Button btnKeyboard9;
	private Button btnKeyboard0;
	private Button btnKeyboardPoint;
	private Button btnKeyboardBack;
	private Button btnKeyboardClear;
	private Button btnKeyboardOk;
	private Button btnKeyboardCancel;
	private Button btnKeyboardAbc;
	
	private KeyBoardListener keyBoardListener = new KeyBoardListener();
	
	
	private int lastX;
	private int lastY;
	private int dx;
	private int dy;
	private int  mScreenX = 0;
	private int  mScreenY = 0;
	
	
	
	
    public EtZdyActivity(EditText et) {
		this.et = et;
	}


	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.et_zdy);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN 
    | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);


        
        
        et = (EditText)findViewById(R.id.et);
        et.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				showPopupWindow();
				return true;
			}
		});
        
    }

    private void showPopupWindow() {
		View view = getLayoutInflater().inflate(R.layout.popup_window, null);
		final PopupWindow popupWindow = new PopupWindow(this);
		if (popupWindow != null) {
		    popupWindow.setFocusable(true);//要先让popupwindow获得焦点,才能正确获取popupwindow的状态
		}
		if (popupWindow.isShowing()) {
		    popupWindow.dismiss();
		}
		popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		popupWindow.setContentView(view);
		
		tv = (TextView)view.findViewById(R.id.tv_pop);
		//获取按钮
		btnKeyboard1 = (Button)view.findViewById(R.id.btn_keyboard_1);
		btnKeyboard2 = (Button)view.findViewById(R.id.btn_keyboard_2);
		btnKeyboard3 = (Button)view.findViewById(R.id.btn_keyboard_3);
		btnKeyboard4 = (Button)view.findViewById(R.id.btn_keyboard_4);
		btnKeyboard5 = (Button)view.findViewById(R.id.btn_keyboard_5);
		btnKeyboard6 = (Button)view.findViewById(R.id.btn_keyboard_6);
		btnKeyboard7 = (Button)view.findViewById(R.id.btn_keyboard_7);
		btnKeyboard8 = (Button)view.findViewById(R.id.btn_keyboard_8);
		btnKeyboard9 = (Button)view.findViewById(R.id.btn_keyboard_9);
		btnKeyboard0 = (Button)view.findViewById(R.id.btn_keyboard_0);
		btnKeyboardPoint = (Button)view.findViewById(R.id.btn_keyboard_point);
		btnKeyboardBack = (Button)view.findViewById(R.id.btn_keyboard_back);
		btnKeyboardClear = (Button)view.findViewById(R.id.btn_keyboard_clear);
		btnKeyboardOk = (Button)view.findViewById(R.id.btn_keyboard_ok);
		btnKeyboardCancel = (Button)view.findViewById(R.id.btn_keyboard_cancel);
		btnKeyboardAbc = (Button)view.findViewById(R.id.btn_keyboard_abc);
		
		btnKeyboard1.setOnClickListener(keyBoardListener);
		btnKeyboard2.setOnClickListener(keyBoardListener);
		btnKeyboard3.setOnClickListener(keyBoardListener);
		btnKeyboard4.setOnClickListener(keyBoardListener);
		btnKeyboard5.setOnClickListener(keyBoardListener);
		btnKeyboard6.setOnClickListener(keyBoardListener);
		btnKeyboard7.setOnClickListener(keyBoardListener);
		btnKeyboard8.setOnClickListener(keyBoardListener);
		btnKeyboard9.setOnClickListener(keyBoardListener);
		btnKeyboard0.setOnClickListener(keyBoardListener);
		btnKeyboardPoint.setOnClickListener(keyBoardListener);
		btnKeyboardBack.setOnClickListener(keyBoardListener);
		btnKeyboardClear.setOnClickListener(keyBoardListener);
		btnKeyboardOk.setOnClickListener(keyBoardListener);
		btnKeyboardAbc.setOnClickListener(keyBoardListener);
		
		
		//PopupWindow进行拖动
		view.findViewById(R.id.lay).setOnTouchListener(new OnTouchListener()
	    {
	        public boolean onTouch(View v, MotionEvent event)
	        {
	        	 int action = event.getAction();
	                switch (action) {
	                case MotionEvent.ACTION_DOWN:
	                    lastX = (int) event.getRawX();
	                    lastY = (int) event.getRawY();
	                    break;
	                case MotionEvent.ACTION_UP:
	                    mScreenX = dx;
	                    mScreenY = dy;
	                    break;
	                case MotionEvent.ACTION_MOVE:
	                    dx = (int) event.getRawX() - lastX + mScreenX;
	                    dy =  lastY - (int)event.getRawY() + mScreenY;
	                    popupWindow.update( dx,-dy, -1, -1);
	                    break;
	                }
	                return true;
	    }
	    });
		
		btnKeyboardCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//隐藏PopupWindow
                popupWindow.dismiss();
			}
		});
		
		
		btnKeyboardOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//这里写通过查询数据库得到的数据,判断输入的et.getText()和查询的信息是否符合,若不符合要求则在textView中显示提示信息
				if(!tv.getText().toString().equals("")){
					et.addTextChangedListener(new TextWatcher() {
						
						@Override
						public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
							tv.setText("");
						}
						
						@Override
						public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
								int arg3) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void afterTextChanged(Editable arg0) {
							tv.setText("");
							
						}
					});
				}else{
					//隐藏PopupWindow
	                popupWindow.dismiss();
				}
			}
		});
		
		popupWindow.showAsDropDown(et);  //设置PopupWindow弹出位置在et下边
		
	}

    
    class KeyBoardListener  implements View.OnClickListener{
    	@Override
    	public void onClick(View v) {
    		String str = et.getText().toString();
    		int length = str.length();
    		switch (v.getId()) {
    		case R.id.btn_keyboard_1:
    		case R.id.btn_keyboard_2:
    		case R.id.btn_keyboard_3:
    		case R.id.btn_keyboard_4:
    		case R.id.btn_keyboard_5:
    		case R.id.btn_keyboard_6:
    		case R.id.btn_keyboard_7:
    		case R.id.btn_keyboard_8:
    		case R.id.btn_keyboard_9:
    		case R.id.btn_keyboard_0:
    		case R.id.btn_keyboard_point:
    			et.setText(str+((Button)v).getText());  
    			break;
    		case R.id.btn_keyboard_back:
    			if(!str.equals("")){
        			et.setText(str.substring(0,length-1));
    			}
    			break;
    		case R.id.btn_keyboard_clear:
    			et.setText("");
    			break;
    		case R.id.btn_keyboard_abc:
    			//待定
    			break;
    		default:
    			break;
    		}
    		
    	}
    }
    
    private String check(String cxtj){
    	String str = "";
		String sql = “写你的sql语句”;
		try {
			SQLiteDatabase db = SQLiteDatabase.openDatabase(dbfile, null, SQLiteDatabase.OPEN_READWRITE);  //这里使用的是sqlite数据库
			Cursor c = db.rawQuery(sql, null);
			if(c.moveToFirst()){
				str = c.getString(0);
			}
			c.close();
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
    }
    
}

et_zdy.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:orientation="vertical" >
    <EditText 
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"/>
        
</LinearLayout>

popup_window.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/lay">
    <TextView 
        android:id="@+id/tv_pop"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:textSize="16sp"
        android:textColor="#000"
        android:text=""/>
 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
 
        <Button
            android:id="@+id/btn_keyboard_1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="1"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_2"
			android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="2"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_3"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="3"
            android:textColor="#fff"
            android:textSize="40sp" />
        <Button
            android:id="@+id/btn_keyboard_cancel"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="取消"
            android:textColor="#fff"
            android:textSize="20sp" />
 
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="horizontal" >
 
        <Button
            android:id="@+id/btn_keyboard_4"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="4"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_5"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="5"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_6"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="6"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        
        <Button
            android:id="@+id/btn_keyboard_abc"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="abc"
            android:textColor="#fff"
            android:textSize="22sp" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="horizontal" >
 
        <Button
            android:id="@+id/btn_keyboard_7"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="7"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_8"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="8"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_9"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="9"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_back"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="后退"
            android:textColor="#fff"
            android:textSize="20sp" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="horizontal" >
 
        <Button
            android:id="@+id/btn_keyboard_clear"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:paddingTop="8dp"
            android:text="清空"
            android:textColor="#fff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/btn_keyboard_0"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="0"
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_point"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:text="."
            android:textColor="#fff"
            android:textSize="40sp" />
 
        <Button
            android:id="@+id/btn_keyboard_ok"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:paddingTop="8dp"
            android:text="确定"
            android:textColor="#fff"
            android:textSize="20sp" />
    </LinearLayout>
 
</LinearLayout>

参考1:小键盘
https://blog.csdn.net/sinat_30114673/article/details/51460614?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

参考2:软键盘
普通基础自定义键盘(中英文兼顾)、每次弹出都会改变数字顺序的自定义密码键盘、能与webview交互的自定义密码键盘
https://blog.csdn.net/wozuihaole/article/details/80364296?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.nonecase

参考3:软键盘
https://www.jianshu.com/p/8f3382a63f8b

参考4:软键盘(该博主有git代码分享喔~)
https://blog.csdn.net/SValence/article/details/93901651?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.opensearch_close&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.opensearch_close

参考5:改变数字顺序的密码软键盘(有git代码分享)
https://www.jianshu.com/p/09dfdface4af

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐