禁用系統軟鍵盤方法1: android
在你的Activity的onCreate()方法中加入如下代碼: ide
EditText edittext=(EditText )findViewById(R.id.xx); 佈局
edittext.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL); 字體
禁用系統軟鍵盤方法2: this
在你的Activity的onCreate()方法中加入如下代碼: 接口
EditText edittext=(EditText )findViewById(R.id.xx); 事件
edittext.setKeyListener(null); 開發
若是須要偵聽遙控器數字按鍵,上面句需改爲edittext.setKeyListener(this);而後在你的activity裏實現KeyListener接口,接口方法貼出: get
@Override it
public void clearMetaKeyState(View view, Editable editable, int i) {
// TODO Auto-generated method stub
}
@Override
public int getInputType() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean onKeyDown(View view, Editable editable, int i,
KeyEvent keyevent) {
if (keyevent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
hideSoftKeyBoard(true); ③
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_0) {
numBtnVal = "0";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_1) {
numBtnVal = "1";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_2) {
numBtnVal = "2";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_3) {
numBtnVal = "3";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_4) {
numBtnVal = "4";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_5) {
numBtnVal = "5";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_6) {
numBtnVal = "6";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_7) {
numBtnVal = "7";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_8) {
numBtnVal = "8";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_9) {
numBtnVal = "9";
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_CTRL_LEFT) {
if (curFocusPosi > 0) {
curFocusPosi--;
} else {
curFocusPosi = 0;
}
setEditTextVal();
return true;
} else if (keyevent.getKeyCode() == KeyEvent.KEYCODE_CTRL_RIGHT) {
if (curFocusPosi < tmpSNVal.length()) {
curFocusPosi++;
}
setEditTextVal();
return true;
} else {
return false;
}
}
@Override
public boolean onKeyOther(View view, Editable editable, KeyEvent keyevent) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onKeyUp(View view, Editable editable, int i,
KeyEvent keyevent) {
// TODO Auto-generated method stub
return true;
}
其中紅色標出的 ③處是彈出咱們本身的鍵盤。自定義鍵盤的全部button放在id爲softInputArea的相對佈局中,並實現按鈕的點擊事件numBtnClickHandler(View source),從而改變edittext的內容。
private void hideSoftKeyBoard(boolean showOrHide) {
RelativeLayout keyBoardLayout = (RelativeLayout) findViewById(R.id.softInputArea);
if (keyBoardLayout != null) {
if (showOrHide) {
keyBoardLayout.setVisibility(View.GONE);
} else {
keyBoardLayout.setVisibility(View.VISIBLE);
}
}
}
部分自定義鍵盤佈局:
<RelativeLayout
android:id="@+id/softInputArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/ic_keyboard_backgroud"
android:visibility="visible" >
//數字1
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/number1_button_style"
android:focusable="true"
android:onClick="numBtnClickHandler" />
//數字2
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn1"
android:background="@drawable/number2_button_style"
android:focusable="true"
android:onClick="numBtnClickHandler" />
。。。。。。咱們必須在activity中實現點擊事件的自定義方法 numBtnClickHandler(View source)方法,經過形參View source咱們能夠得知是哪一個按鈕被點擊,而後對各個按鈕進行處理,同時給edittext賦值!!
public void numBtnClickHandler(View source) {
if (source != null) {
if (source.getId() == R.id.btn0) {
numBtnVal = "0";
} else if (source.getId() == R.id.btn1) {
numBtnVal = "1";
} else if (source.getId() == R.id.btn2) {
numBtnVal = "2";
} else if (){.......}