android默认键盘布局
我正在为Nexus7开发一个应用程序,我需要特定的EditText来显示带有数字和特殊字符的键盘视图。我知道您可以使用inputType设置EditText的布局,但我的问题是,如果我设置inputType=“number”,则会出现拨号板,无法切换到字符视图。我所需要的(是客户的请求)是按左下角的“123”键打开键盘,键盘布局如图所示。 我尝试了setRawInputType和setInputType的所有组合,但都没有成功。 此组合显示拨号板android默认键盘布局,android,android-softkeyboard,android-keypad,keyboard-layout,Android,Android Softkeyboard,Android Keypad,Keyboard Layout,我正在为Nexus7开发一个应用程序,我需要特定的EditText来显示带有数字和特殊字符的键盘视图。我知道您可以使用inputType设置EditText的布局,但我的问题是,如果我设置inputType=“number”,则会出现拨号板,无法切换到字符视图。我所需要的(是客户的请求)是按左下角的“123”键打开键盘,键盘布局如图所示。 我尝试了setRawInputType和setInputType的所有组合,但都没有成功。 此组合显示拨号板 txtLineCode.setInputType
txtLineCode.setInputType(InputType.TYPE_CLASS_TEXT);
txtLineCode.setRawInputType(InputType.TYPE_CLASS_NUMBER);
这个组合也显示拨号板
txtLineCode.setInputType(InputType.TYPE_CLASS_TEXT);
txtLineCode.setRawInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL|InputType.TYPE_CLASS_NUMBER);
下面是一些截图,可以更好地解释我需要什么
这是默认键盘
这是我单击“?123”时显示的布局,这是我默认需要显示的布局
如果我设置inputType=“number”,则显示的布局不允许切换到选择器布局
当我看到一些编辑文本通常是数字,但应该包含数字时,我该怎么办
非常感谢我想我找到了一个非常优雅的解决方案: 我在文本框中使用了一个Drawable(在我的例子中是drawableRight),并在Drawable上分配了一个click侦听器,它执行数字模式和文本模式之间的切换。 我可以使用以下技巧在drawable上分配一个侦听器: 结果是:当EditText第一次显示时,显示如下 点击“ABC”图像就会变成这样
希望这能帮助某人记住,如果你能让它工作(我怀疑),用户可以安装不同的键盘,它们的行为完全不同。是的,我知道,但我的目标是确保普通键盘的正确行为。为什么您怀疑它是否能正常工作?如果您让它显示?123下的键,那么如何阻止用户切换回。尝试一个自定义键盘我不需要阻止用户切换,我需要允许切换,但客户希望按键默认在?123以下,因为95%的次数都是数字,但也可能包含字母。您可能只需要使用特定的按键代码向键盘发送按键。这不是最好的解决方案,但可能会奏效。
public class MyEdittextextends EditText {
private Drawable drawableRight;
private Drawable drawableLeft;
private Drawable drawableTop;
private Drawable drawableBottom;
//YOUR STUFF HERE
@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
if (right != null) {
drawableRight = right;
}
if (left != null) {
drawableLeft = left;
}
super.setCompoundDrawables(left, top, right, bottom);
}
View.OnClickListener _leftDrawableClickListener = null;
View.OnClickListener _rightDrawableClickListener = null;
public void setLeftDrawableClickListener(View.OnClickListener clickListener) {
_leftDrawableClickListener = clickListener;
}
public void setRightDrawableClickListener(View.OnClickListener clickListener) {
_rightDrawableClickListener = clickListener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x, y;
Rect bounds;
x = (int) event.getX();
y = (int) event.getY();
// this works for left since container shares 0,0 origin with bounds
if (drawableLeft != null) {
bounds = drawableLeft.getBounds();
if (bounds.contains(x - fuzz, y - fuzz)) {
try {
_leftDrawableClickListener.onClick(this);
} catch (Exception e) {
}
if (consumeEvent) {
event.setAction(MotionEvent.ACTION_CANCEL);
return false;
}
}
} else if (drawableRight != null) {
bounds = drawableRight.getBounds();
if (x >= (this.getRight() - bounds.width() - fuzz) && x <= (this.getRight() - this.getPaddingRight() + fuzz) && y >= (this.getPaddingTop() - fuzz) && y <= (this.getHeight() - this.getPaddingBottom()) + fuzz) {
try {
_rightDrawableClickListener.onClick(this);
} catch (Exception e) {
}
if (consumeEvent) {
event.setAction(MotionEvent.ACTION_CANCEL);
return false;
}
}
} else if (drawableTop != null) {
// not implemented yet
} else if (drawableBottom != null) {
// not implemented yet
}
}
return super.onTouchEvent(event);
}
@Override
protected void finalize() throws Throwable {
drawableRight = null;
drawableBottom = null;
drawableLeft = null;
drawableTop = null;
super.finalize();
}
}
myEdittext = (EditText) findViewById(R.id.myEdittext);
myEdittext.setRightDrawableClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (myEdittext.getInputType() != InputType.TYPE_CLASS_TEXT) {
myEdittext.setInputType(InputType.TYPE_CLASS_TEXT);
myEdittext.setRawInputType(InputType.TYPE_CLASS_TEXT);
myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_123, 0);
} else {
myEdittext.setRawInputType(InputType.TYPE_CLASS_NUMBER);
myEdittext.setInputType(InputType.TYPE_CLASS_NUMBER);
myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_abc, 0);
}
}
});