展会信息港展会大全

android EditText(文本输入框)去掉系统软键盘的方法
来源:互联网   发布日期:2016-01-06 19:13:15   浏览:2816次  

导读:默认状态下,当我们点击文本输入框时,会弹出系统软键盘。当在大屏幕的android设备上有输入需求时,系统软键盘的已输入文本会感觉字体有些小,不容易看清。本人也未找到方大软键盘输入时文本的方法。只好通过禁用......

默认状态下,当我们点击文本输入框时,会弹出系统软键盘。 当在大屏幕的android设备上有输入需求时,系统软键盘的已输入文本会感觉字体有些小,不容易看清。本人也未找到方大软键盘输入时文本的方法。只好通过禁用系统软键盘,开发自己的键盘的方法补救!

禁用系统软键盘方法1:

在你的Activity的onCreate()方法中加入以下代码:

EditText edittext=(EditText )findViewById(R.id.xx);

edittext.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL);

禁用系统软键盘方法2:

在你的Activity的onCreate()方法中加入以下代码:

EditText edittext=(EditText )findViewById(R.id.xx);

edittext.setKeyListener(null);

如果需要侦听遥控器数字按键,上面句需改成edittext.setKeyListener(this);然后在你的activity里实现KeyListener接口,接口方法贴出:

@Override

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 (){.......}

赞助本站

人工智能实验室

相关热词: EditText 软键盘

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港