我的应用程序中有一个 EditText,它仅接收来自我放置在屏幕上的按钮的输入。
为了避免出现软键盘,我有一个自定义的 EditText 类,如下所示:
public class CustomEditText extends EditText {
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
// Disables Keyboard;
public boolean onCheckIsTextEditor() {
return false;
}
}
这成功地阻止了键盘的出现,但是在 ICS 中,这种方法也阻止了光标的出现。
setCursorVisible(true)
没有任何影响。
我尝试过隐藏软键盘的替代方法,例如使用android:editable="false"
and .setKeyListener(null);
但这些解决方案在我的测试中都不起作用。键盘始终出现。
那么,有没有办法在 ICS 中返回光标,同时保持 onCheckIsTextEditor 覆盖不变?
你为什么不尝试像这样禁用软键盘
PINLockactivity.java
//text field for input sequrity pin
txtPin=(EditText) findViewById(R.id.txtpin);
txtPin.setInputType(
InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
txtPin.setSelection(txtPin.getText().length());
txtPin.setTextSize(22);
txtPin.setSingleLine(true);
//disable keypad
txtPin.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = txtPin.getInputType(); // backup the input type
txtPin.setInputType(InputType.TYPE_NULL); // disable soft input
txtPin.onTouchEvent(event); // call native handler
txtPin.setInputType(inType); // restore input type
return true; // consume touch even
}
});
对于这个 EditText 字段
xml代码是
<EditText android:layout_width="wrap_content"
android:id="@+id/txtpin"
android:maxLength="4"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:longClickable="false"
android:padding="2dp"
android:inputType="textPassword|number"
android:password="true"
android:background="@drawable/edittext_shadow"
android:layout_weight="0.98"
android:layout_marginLeft="15dp">
<requestFocus></requestFocus>
</EditText>
这对我来说工作正常,可以用光标输入安全 PIN 码。
我从按钮而不是键盘获取输入。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)