精华内容
下载资源
问答
  • Android自定义软键盘

    千次下载 热门讨论 2012-08-29 15:09:21
    Android自定义软键盘
  • android自定义软键盘

    2013-09-27 21:04:00
    android 自定义 软键盘
  • android 自定义软键盘

    2018-05-06 10:43:52
    自定义纯数字软键盘 大气美观简洁 纯白色数字键盘(仿IOS 支付宝微信支付键盘EX_测试自定义纯数字软键盘
  • Android 自定义软键盘

    2017-02-24 10:33:08
    Android 自定义软键盘  定义软键盘,先看效果图:  各位可以自行更改样式颜色。  那么接下来就是如可定义:  1.定义键盘布局,你可以有好几套布局(纯数字,纯符号,英文小写,英文大写等),我这里...
    Android 自定义软键盘
      定义软键盘,先看效果图:


     各位可以自行更改样式颜色。
     那么接下来就是如可定义:
      1.定义键盘布局,你可以有好几套布局(纯数字,纯符号,英文小写,英文大写等),我这里只写了两套布局:
      在 res 下新建 xml 目录
      
    <?xml version="1.0" encoding="utf-8"?>
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?xml version="1.0" encoding="utf-8"?>
    

      2.新建activity布局:
      
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <?xml version="1.0" encoding="UTF-8"?>
    

      3.KeyboardUtil及activity调用:
      
    package com.huyu.test;
    
    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.util.List;
    
    import android.app.Activity;
    import android.inputmethodservice.Keyboard;
    import android.inputmethodservice.Keyboard.Key;
    import android.inputmethodservice.KeyboardView;
    import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
    import android.text.Editable;
    import android.text.InputType;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.EditText;
    
    public class KeyboardUtil {
    	private KeyboardView keyboardView;
    	private Keyboard k1;// 字母键盘
    	private Keyboard k2;// 数字键盘
    	public boolean isnun = false;// 是否数据键盘
    	public boolean isupper = false;// 是否大写
    	public boolean isShow = false;// 是否显示
    	
    
    	private EditText ed;
    
    	public KeyboardUtil(EditText edit) {
    		this.ed = edit;
    		Activity act = (Activity) this.ed.getContext();
    		k1 = new Keyboard(act, R.xml.letter);
    		k2 = new Keyboard(act, R.xml.symbol);
    		
    		keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
    		keyboardView.setKeyboard(k2);
    		keyboardView.setEnabled(true);
    		keyboardView.setPreviewEnabled(true);//设置键盘提示的view  
    		keyboardView.setOnKeyboardActionListener(listener);
    		
    		setSoftKeyboradHide(act, edit);
    	}
    
    	/**
    	 * 这是在处理光标出现的问题
    	 * @param ctx
    	 * @param edit
    	 */
    	private void setSoftKeyboradHide(Activity ctx,EditText edit){
    //		if (android.os.Build.VERSION.SDK_INT <= 10) { 
    //			edit.setInputType(InputType.TYPE_NULL); 
    //		} else {
    			ctx.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    			try { 
    				Class cls = EditText.class; 
    				Method setShowSoftInputOnFocus = null; 
    				String  method_setSoftInputOnFocus = "setShowSoftInputOnFocus";
    				//4.0的是setShowSoftInputOnFocus,4.2的是setSoftInputOnFocus	
    				if(android.os.Build.VERSION.SDK_INT>16) {
    					method_setSoftInputOnFocus = "setSoftInputOnFocus";
    				}
    				setShowSoftInputOnFocus = cls.getMethod(method_setSoftInputOnFocus, boolean.class); 
    				setShowSoftInputOnFocus.setAccessible(false); 
    				setShowSoftInputOnFocus.invoke(edit, false); 
    			} catch (Exception e) { 
    				e.printStackTrace(); 
    			}
    //		}
    	}
    
    	private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
    		@Override
    		public void swipeUp() {
    		}
    
    		@Override
    		public void swipeRight() {
    		}
    
    		@Override
    		public void swipeLeft() {
    		}
    
    		@Override
    		public void swipeDown() {
    		}
    
    		@Override
    		public void onText(CharSequence text) {
    		}
    
    		@Override
    		public void onRelease(int primaryCode) {
    		}
    
    		@Override
    		public void onPress(int primaryCode) {
    			checkIShowPrewiew(primaryCode);
    		}
    
    		@Override
    		public void onKey(int primaryCode, int[] keyCodes) {
    			checkIShowPrewiew(primaryCode);
    			
    			Editable editable = ed.getText();
    			int start = ed.getSelectionStart();
    			if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
    				hideKeyboard();
    			} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 删除
    				if (editable != null && editable.length() > 0) {
    					if (start > 0) {
    						editable.delete(start - 1, start);
    					}
    				}
    			} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
    				changeKey();
    				keyboardView.setKeyboard(k2);
    
    			} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
    				if (isnun) {
    					isnun = false;
    					keyboardView.setKeyboard(k1);
    				} else {
    					isnun = true;
    					keyboardView.setKeyboard(k2);
    				}
    			} else if (primaryCode == 57419) { // 左移
    				if (start > 0) {
    					ed.setSelection(start - 1);
    				}
    			} else if (primaryCode == 57421) { // 右移
    				if (start < ed.length()) {
    					ed.setSelection(start + 1);
    				}
    			} else {
    				editable.insert(start, Character.toString((char) primaryCode));
    			}
    		}
    	};
    	
    	/**
    	 * 除了 删除,完成,大小写切换,. 空格 ,等(可自己定义) 这些符号不用开启预览,其他需要预览
    	 */
    	private void checkIShowPrewiew(int primaryCode) {
    		List list = Arrays.asList(Keyboard.KEYCODE_CANCEL,
    				Keyboard.KEYCODE_DELETE,
    				Keyboard.KEYCODE_SHIFT,
    				46, 32, 44, -2);
    		if(list.contains(primaryCode)){
    			keyboardView.setPreviewEnabled(false);
    		}else{
    			keyboardView.setPreviewEnabled(true);
    		}
    	}
    	
    	/**
    	 * 键盘大小写切换
    	 */
    	private void changeKey() {
    		List keylist = k1.getKeys();
    		if (isupper) {//大写切换小写
    			isupper = false;
    			for(Key key:keylist){
    				if (key.label!=null && isword(key.label.toString())) {
    					key.label = key.label.toString().toLowerCase();
    					key.codes[0] = key.codes[0]+32;
    				}
    			}
    		} else {//小写切换大写
    			isupper = true;
    			for(Key key:keylist){
    				if (key.label!=null && isword(key.label.toString())) {
    					key.label = key.label.toString().toUpperCase();
    					key.codes[0] = key.codes[0]-32;
    				}
    			}
    		}
    	}
    
        public void showKeyboard() {
            int visibility = keyboardView.getVisibility();
            if (visibility == View.GONE || visibility == View.INVISIBLE) {
                keyboardView.setVisibility(View.VISIBLE);
                isShow = true;
            }
        }
        
        public void hideKeyboard() {
            int visibility = keyboardView.getVisibility();
            if (visibility == View.VISIBLE) {
                keyboardView.setVisibility(View.INVISIBLE);
                isShow = false;
            }
        }
        
        private boolean isword(String str){
        	String wordstr = "abcdefghijklmnopqrstuvwxyz";
        	if (wordstr.indexOf(str.toLowerCase())>-1) {
    			return true;
    		}
        	return false;
        }
    
    }
    public class MainActivity extends  Activity {
    
    	private KeyboardUtil keyboardUtil;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.keyboard_layout);
    		
    		final EditText et = (EditText) findViewById(R.id.edit1);
    		et.setOnTouchListener(new OnTouchListener() {
    			
    			@Override
    			public boolean onTouch(View v, MotionEvent event) {
    				int inputType = et.getInputType();
    				et.setInputType(InputType.TYPE_NULL);
    				if(null == keyboardUtil)
    					keyboardUtil = new KeyboardUtil(et);
    				
    				if(!keyboardUtil.isShow)
    					keyboardUtil.showKeyboard();
    				et.setInputType(inputType);
    				return false;
    			}
    		});
    	}
    	
    	@Override
    	public void onBackPressed() {
    		if(keyboardUtil.isShow)
    			keyboardUtil.hideKeyboard();
    		super.onBackPressed();
    		
    	}
    
    }
    
      
      可能需要的图片:
      keyboard_delete.png keyboard_left.png keyboard_right.png keyboard_shift.png keyboard_space.png normal_key_bg.9.png normal_key_hl_bg.9.png












    展开全文
  • Android自定义软键盘KeyboardView
  • android自定义软键盘键盘 系统键盘源码 两款android软件键盘 供大家参考学习
  • 本篇文章主要介绍了 Android自定义软键盘的设计与实现代码,有需要的可以了解一下。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 545
精华内容 218
关键字:

android自定义软键盘