android EditText(文本输入框)去掉 系统软件盘的方法

本文介绍了两种禁用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 (){.......}

转载于:https://my.oschina.net/zhangjie830621/blog/95698

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值