好的,第一次发表海报。我在高中任教,第一年在工作上没有导师帮忙,所以如果我的代码是垃圾,请温柔。
问题::这个程序是为了保持销售poptarts的库存。在XML中,我有一个线性布局,listview,最后是一个线性布局。列表视图通过,使用自定义适配器显示当前的pop-tarts并具有更新计数的编辑文本。最后一个线性布局具有编辑文本,以向arraylist添加新的poptart。在我添加此代码之后,每当我在listview中选择编辑文本时(我被强制为input_type:" number"),数字键盘会弹出然后消失,被正常的qwerty键盘取代编辑文本框失去焦点。如果我快速点击编辑文本框3-4次,它将保持数字键盘向上并保持焦点。我无法弄清楚为什么焦点和键盘会被改变。
<LinearLayout
...
<TextView
...
<TextView
...
<TextView
...
<TextView
...
<TextView
...
</LinearLayout>
<ListView
android:layout_below="@+id/flavor"
android:id="@+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"
android:layout_weight="1"
android:layout_gravity="top" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_weight="1">
<EditText
android:id="@+id/newFlavor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Flavor"
android:textSize="10pt"
/>
<CheckBox
android:id="@+id/isSeasonal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:checked="false"
/>
<EditText
android:id="@+id/newCurrent"
android:layout_width="0dp"
android:maxLength="3"
android:layout_height="wrap_content"
android:hint="Current"
android:textSize="10pt"
/>
<EditText
android:id="@+id/newMax"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLength="3"
android:hint="Max"
android:textSize="10pt"
/>
<Button
android:id="@+id/addNewFlavor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="10pt"
android:text="Add"
android:layout_weight="1" />
</LinearLayout>
list_view_inventory.xml内部:
...
<EditText
android:id="@+id/edit_text"
android:maxLength="3"
android:paddingLeft="25dp"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:layout_weight="1" />
...
最后我发布MyAdapter:
public class MyAdapter extends ArrayAdapter<PopTart> {
ArrayList<PopTart> popTartList = new ArrayList<>();
Button buttonOne;
CheckBox seasonal;
public MyAdapter(Context context, int textViewResourceId, ArrayList<PopTart> tarts){
super(context, textViewResourceId, tarts);
popTartList = tarts;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_inventory, null);
TextView nameView = (TextView) v.findViewById(R.id.name);
final TextView countView = (TextView) v.findViewById(R.id.count);
final EditText countUpdate = (EditText) v.findViewById(R.id.edit_text);
CheckBox seasonal = (CheckBox) v.findViewById(R.id.seasonal);
//Tried to force the input type again here programmatically
countUpdate.setInputType(InputType.TYPE_CLASS_NUMBER);
nameView.setText(popTartList.get(position).getName());
countView.setText(String.valueOf(popTartList.get(position).getCount()));
seasonal.setChecked(popTartList.get(position).getmSeasonal());
seasonal.setEnabled(false);
buttonOne = (Button) v.findViewById(R.id.increment);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (countUpdate.getText().toString().trim().length() == 0)
{
//Making sure the EditText isn't empty
}
else {
String temp2 = countUpdate.getText().toString();
PopTart temp = popTartList.get(position);
temp.setCount(Integer.valueOf(temp2));
popTartList.set(position, temp);
countView.setText(String.valueOf(popTartList.get(position).getCount()));
countUpdate.setText(""); }
}
});
return v;
}
}
感谢您所做的一切,Stack Overflow社区!你已经教过我很多了!
答案 0 :(得分:1)
在Android 7中,我没有看到这个问题,但我最近在旧版Android上也遇到了这个问题。
我通过添加以下三个来解决这个问题。
1:在listView上设置 android:descendantFocusability =“afterDescendants”。
FOCUS_AFTER_DESCENDANTS
在API级别1中添加 int FOCUS_AFTER_DESCENDANTS
只有当它的后代都不想要它时,这个视图才会得到关注。
2:将以下内容添加到EditText中
的机器人:可聚焦= “真”
android:focusableInTouchMode =“true”
3:将 android:windowSoftInputMode =“adjustResize”添加到清单文件的活动中。因此,即使出现键盘,您的列表也可以滚动。
答案 1 :(得分:0)
我认为您的list_view_inventory.xml
应如下所示,只需在android:inputType="number"
中添加EditText
...
<EditText
android:id="@+id/edit_text"
android:maxLength="3"
android:paddingLeft="25dp"
android:layout_width="75dp"
android:inputType="number"
android:layout_height="wrap_content"
android:paddingRight="30dp"
android:layout_weight="1" >
<requestFocus />
</EditText>
...
或以编程方式请求焦点。
edittext.requestFocus();
你甚至可以删除在你的适配器的getView()
方法中将输入类型设置为EditText的代码。
引用Android文档
每当ListView需要显示特定的数据行时,它就会请求相关的适配器通过getView()方法提供与该位置的数据相对应的视图。每当需要在屏幕上更新视图时(例如,在创建/滚动等期间),都可能发生这种情况。
因此,只要调用inputType= "number"
,就会指定getView()
,然后它会回退到默认的inputtype并弹出qwerty键盘。
我认为它会解决你的问题。
所以你可以使用以下
android:focusableInTouchMode="true"
要获得所需结果,请删除之前的<requestFocus/>