我一直在尝试使用一些编辑文本来实现listview,这些编辑文本在聚焦时选择所有文本。在找到这篇文章之前,没有任何工作:https://stackoverflow.com/questions/24162604/android-edittext-in-listview-with-selection?answertab=active#tab-top。我实现了显示的代码,我遇到了一个小问题。当用户首次打开活动并点击编辑文本时,它将不会选择所有文本,但是如果您点击另一个编辑文本然后点击上一个文本,它将选择文本就好了。有人有什么想法吗?
以下是一些代码:
public class StandSheetCursorAdapter extends CursorAdapter {
private boolean isStartButtonShowing;
private boolean isEndButtonShowing;
private String firstColumnKey;
private String secondColumnKey;
private EditText focusedEditText;
private EditText selectAllFocusedEditText;
private View.OnClickListener mOnClickListener;
private Context mContext = null;
private Map<Integer, ContentValues> contentValuesMap = Maps.newHashMap();
private View.OnClickListener minusListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText = (EditText) view.getTag(R.id.edit_text_key);
updateEditText(editText, false);
}
};
private View.OnClickListener plusListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText = (EditText) view.getTag(R.id.edit_text_key);
updateEditText(editText, true);
}
};
private TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length()==1){
selectAllFocusedEditText = null;
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};
public StandSheetCursorAdapter(Context context, Cursor cursor, int flags, int standSheetType){
super(context, cursor, flags);
switch (standSheetType){
case 0:
firstColumnKey= InventoryContract.StockItem.CursorColumns.ACTUAL_START_COUNT;
secondColumnKey= InventoryContract.StockItem.CursorColumns.ACTUAL_END_COUNT;
break;
case 1:
firstColumnKey= InventoryContract.StockItem.CursorColumns.TRANSFER_IN_COUNT;
secondColumnKey= InventoryContract.StockItem.CursorColumns.TRANSFER_OUT_COUNT;
break;
case 2:
firstColumnKey= InventoryContract.StockItem.CursorColumns.GRATIS_COUNT;
secondColumnKey=InventoryContract.StockItem.CursorColumns.WASTE_COUNT;
break;
}
// Cache context for use in click listeners
mContext = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.standsheet_type_list_item, parent, false);
view.findViewById(R.id.column2_item_shim).setOnClickListener(mOnClickListener);
view.findViewById(R.id.column3_item_shim).setOnClickListener(mOnClickListener);
return view;
}
public void setOnClickListener(View.OnClickListener listener) {
mOnClickListener = listener;
}
private int getCount(Integer itemIndex, String columnKey){
int startCountNum=0;
Cursor c = mContext.getContentResolver().query(InventoryContract.StockItem.getStockItemUri(itemIndex), null, null, null, null);
if (c != null) {
if (c.moveToFirst()) {
startCountNum = CursorUtil.getLong(c, columnKey).intValue();
}
c.close();
}
return startCountNum;
}
private View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
EditText et = (EditText) v;
if (!hasFocus) {
focusedEditText = null;
updateEditText(et);
}
else if(hasFocus) {
focusedEditText = (EditText)v;
selectAllFocusedEditText = (EditText)v;
}
}
};
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
Integer index = cursor.getInt(cursor.getColumnIndex(InventoryContract.StockItem.CursorColumns.ID));
TextView itemName=(TextView)view.findViewById(R.id.stock_item_name);
itemName.setText(cursor.getString(cursor.getColumnIndex(InventoryContract.StockItem.CursorColumns.NAME)));
EditText firstColumnET=(EditText)view.findViewById(R.id.column2_et);
firstColumnET.setTag(R.id.index_key, index);
firstColumnET.setTag(R.id.column_key, firstColumnKey);
firstColumnET.setText(cursor.getString(cursor.getColumnIndex(firstColumnKey)));
firstColumnET.setOnFocusChangeListener(focusChangeListener);
firstColumnET.setFocusable(isStartButtonShowing);
firstColumnET.setFocusableInTouchMode(isStartButtonShowing);
firstColumnET.addTextChangedListener(textWatcher);
EditText secondColumnET=(EditText)view.findViewById(R.id.column3_et);
secondColumnET.setTag(R.id.index_key, index);
secondColumnET.setTag(R.id.column_key, secondColumnKey);
secondColumnET.setText(cursor.getString(cursor.getColumnIndex(secondColumnKey)));
secondColumnET.setOnFocusChangeListener(focusChangeListener);
secondColumnET.setFocusable(isEndButtonShowing);
secondColumnET.setFocusableInTouchMode(isEndButtonShowing);
secondColumnET.addTextChangedListener(textWatcher);
secondColumnET.addTextChangedListener(textWatcher);
ImageButton startCountMinButton = (ImageButton)view.findViewById(R.id.column2_minus_btn);
startCountMinButton.setVisibility(isStartButtonShowing ? View.VISIBLE : View.INVISIBLE);
startCountMinButton.setTag(R.id.index_key, index);
startCountMinButton.setTag(R.id.column_key, firstColumnKey);
startCountMinButton.setTag(R.id.edit_text_key, firstColumnET);
startCountMinButton.setOnClickListener(minusListener);
ImageButton startCountPlusButton = (ImageButton)view.findViewById(R.id.column2_plus_btn);
startCountPlusButton.setVisibility(isStartButtonShowing ? View.VISIBLE : View.INVISIBLE);
startCountPlusButton.setTag(R.id.index_key, index);
startCountPlusButton.setTag(R.id.column_key, firstColumnKey);
startCountPlusButton.setTag(R.id.edit_text_key, firstColumnET);
startCountPlusButton.setOnClickListener(plusListener);
ImageButton endCountMinButton = (ImageButton)view.findViewById(R.id.column3_minus_btn);
endCountMinButton.setVisibility(isEndButtonShowing ? View.VISIBLE : View.INVISIBLE);
endCountMinButton.setTag(R.id.index_key, index);
endCountMinButton.setTag(R.id.column_key, secondColumnKey);
endCountMinButton.setTag(R.id.edit_text_key, secondColumnET);
endCountMinButton.setOnClickListener(minusListener);
ImageButton endCountPlusButton = (ImageButton)view.findViewById(R.id.column3_plus_btn);
endCountPlusButton.setVisibility(isEndButtonShowing ? View.VISIBLE : View.INVISIBLE);
endCountPlusButton.setTag(R.id.index_key, index);
endCountPlusButton.setTag(R.id.column_key, secondColumnKey);
endCountPlusButton.setTag(R.id.edit_text_key, secondColumnET);
endCountPlusButton.setOnClickListener(plusListener);
view.findViewById(R.id.column2_item_shim).setVisibility(isStartButtonShowing ? View.GONE : View.VISIBLE);
view.findViewById(R.id.column3_item_shim).setVisibility(isEndButtonShowing ? View.GONE : View.VISIBLE);
if(selectAllFocusedEditText!=null){
selectAllFocusedEditText.requestFocus();
selectAllFocusedEditText.selectAll();
}
}
以下是列表中每个项目的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/list_minimum_height">
<TextView
android:id="@+id/stock_item_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/standard_padding"
android:layout_gravity="right|bottom"
android:textSize="@dimen/text_size_medium"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false" />
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:id="@+id/column2_minus_btn"
android:layout_width="wrap_content"
android:src="@drawable/minus_btn_red"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:focusable="false"
android:text="@string/minus_sign" />
<EditText
android:id="@+id/column2_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/edit_text_minimum_width"
android:textSize="@dimen/text_size_medium"
android:gravity="center"
android:singleLine="true"
android:layout_toRightOf="@id/column2_minus_btn"
android:inputType="number"
android:digits="0123456789"/>
<ImageButton
android:id="@+id/column2_plus_btn"
android:layout_width="wrap_content"
android:src="@drawable/plus_btn_green"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:layout_toRightOf="@id/column2_et"
android:focusable="false"
android:text="@string/plus_sign" />
<View
android:id="@+id/column2_item_shim"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="@id/column2_et"
android:layout_alignBottom="@id/column2_et"
android:layout_alignRight="@id/column2_plus_btn"
android:layout_alignLeft="@id/column2_minus_btn"
android:clickable="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:id="@+id/column3_minus_btn"
android:layout_width="wrap_content"
android:src="@drawable/minus_btn_red"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:focusable="false"
android:text="@string/minus_sign" />
<EditText
android:id="@+id/column3_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="@dimen/edit_text_minimum_width"
android:textSize="@dimen/text_size_medium"
android:gravity="center"
android:singleLine="true"
android:layout_toRightOf="@id/column3_minus_btn"
android:inputType="number"
android:digits="0123456789"/>
<ImageButton
android:id="@+id/column3_plus_btn"
android:layout_width="wrap_content"
android:src="@drawable/plus_btn_green"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:layout_toRightOf="@id/column3_et"
android:focusable="false"
android:text="@string/plus_sign" />
<View
android:id="@+id/column3_item_shim"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="@id/column3_et"
android:layout_alignBottom="@id/column3_et"
android:layout_alignRight="@id/column3_plus_btn"
android:layout_alignLeft="@id/column3_minus_btn"
android:clickable="true"/>
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
我知道这是延迟的,但我有这个问题好几个星期我想发布适用于我的解决方案,以防任何其他用户偶然发现这个帖子(因为这是我遇到的确切问题)。
进入Android Manifest并在您指定的Activity中添加:
机器人:windowSoftInputMode = “adjustPan”
正如您所提到的,您的EditText没有问题,或者您的EditText在点击时选择所有数据 - 两者都发生了。所以你的问题不是requestFocus()或setSelectAllOnFocus()的实现。
当您单击EditText时,ListView将重新调用getView,因为软键盘已重新定义Activity的主窗口以为键盘腾出空间。
通过调用“adjustPan”,您选择不调整Activity的主窗口(第二次调用getView),而是确保无论屏幕上有什么焦点都停留在屏幕上。无论什么焦点都会在必要时平移到屏幕上。
添加这个,希望你会好起来的!同样,这个解决方案适用于那些只有在第二次单击时才能正确选择EditText才能正确选择的人。
答案 1 :(得分:0)
迟到的答案,但在我遇到完全相反的问题时可能会帮助其他人:第一次OnFocusChanged调用期间selectAll()工作,但后续OnFocusChanged调用期间的任何selectAll()都不会。
我只是将selectAll()移动到一个runnable处理程序中,如下所示,然后按预期工作!
@Override
public void onFocusChange(final View edit_text_widget, boolean hasFocus)
{
if (hasFocus)
{
new Handler().post(new Runnable()
{
@Override
public void run()
{
edit_text_widget.selectAll();
}
});
}
}