Android 安卓输入不受尊重
我有一个列表视图,每行有一个EditText。EditText应该收集数字值,所以我尝试关闭字母键盘,只允许用户看到数字键盘 但是,当填充listview并单击列表中的任何EditText时,会出现数字键盘,但随后很快就会被完整的qwerty键盘取代,用户必须切换回数字以输入数字 我尝试过各种编程组合,如:Android 安卓输入不受尊重,android,Android,我有一个列表视图,每行有一个EditText。EditText应该收集数字值,所以我尝试关闭字母键盘,只允许用户看到数字键盘 但是,当填充listview并单击列表中的任何EditText时,会出现数字键盘,但随后很快就会被完整的qwerty键盘取代,用户必须切换回数字以输入数字 我尝试过各种编程组合,如: mPercent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL); 以
mPercent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
以及XML的多种组合,如
android:inputType="phone"
或
但是,无论组合如何,它们的键盘总是快速切换回qwerty并关闭数字键
我想可能是短信观察者的问题。我使用textwatcher来指示键入的内容。我删除它,添加该行edittext中的任何文本,然后添加textwatcher
这是我的自定义适配器的核心:
public View getView(final int position, View inView, ViewGroup parent) {
ViewHolder holder = null;
if (inView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inView = inflater.inflate(mLayoutResource, null);
holder = new ViewHolder();
holder.mPercent = (EditText) inView.findViewById(R.id.percent);
holder.mMaterial = (TextView) inView.findViewById(R.id.name);
inView.setTag(holder);
} else {
holder = (ViewHolder) inView.getTag();
}
mMaterial.moveToPosition(position);
holder.mPercent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
// remove textwatcher on percentage
holder.mPercent.removeTextChangedListener(percentWatcher);
// insert the percentage for this row
final String material = mMaterial.getString(mMaterial.getColumnIndex(OptionsProvider.OPTIONS_DATA));
final EditText percent = holder.mPercent;
percent.setTag(material); // use material and position as keys into which of the many edittexts the textwatcher is watching
percent.setId(position);
holder.mMaterial.setText(material);
// persist any text the user may have typed in the comment box
if (percentages.containsKey(material)) {
percent.setText(percentages.get(material));
}
else {
percent.setText("");
}
// turn on textwatcher
percentWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// save this text and the position in which is resides.
// if the user scrolls before pressing send, only this position should
// contain the text
String text = percent.getText().toString().trim();
if (text.length() > 0) {
if ((position == percent.getId() &&
(material.equals( percent.getTag().toString())))) {
percentages.put(material, text);
Log.d(TAG, "Percentage inserted "+material+"="+text);
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
// force the EditText to stay with 3 digits (max of 100%)
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
int pct = Integer.parseInt(s.toString());
Log.d(TAG, " Material "+(String) percent.getTag()+ " = "+pct);
}
}
};
// add the textwatcher back to the edittext
holder.mPercent.addTextChangedListener(percentWatcher);
有人知道删除/添加textwatcher是否会破坏输入类型吗?这是我应该探索的一条路径,以找出为什么键盘总是回到qwerty?可能是ListView窃取了焦点。尝试将此添加到ListView XML
android:descendantFocusability="afterDescendants"
或在Java中
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
令人惊叹的!在XML中放置属性是有效的。但万一有人想知道,用编程的方式来做同样有效。非常感谢,波扬!它应该在Java中工作,毕竟您的XML是编译成Java的:)顺便说一句,没问题。
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);