【问题标题】:Android: Can an EditText Within a ListView be Focused as Well as the List Item?Android:ListView 中的 EditText 可以像列表项一样被聚焦吗?
【发布时间】:2011-07-20 04:02:22
【问题描述】:

我知道,当您允许将焦点放在 ListView 中的任何“可聚焦”对象上时,它会禁用将焦点放在 ListView 本身上的能力。我的 ListView 项目中有一个 EditText 和几个按钮,但我还希望能够将 ContextMenu 与 ListView 一起使用,因此我需要能够保持焦点。有没有办法覆盖,以便我的 ListView 和 EditText 框都可以接收焦点?我假设如果有可能,它将在自定义适配器中完成。我正在使用 SimpleCursorAdapter 并在 NewView 方法中管理我的按钮点击。我的代码如下:

public class GrainListAdapter extends SimpleCursorAdapter {

private Button upButton;

public GrainListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    int nameColumn = cursor.getColumnIndex("name");
    String getName = cursor.getString(nameColumn);
    TextView name = (TextView)view.findViewById(R.id.GrainName);
    name.setText(getName);

    int originColumn = cursor.getColumnIndex("origin");
    String getOrigin = cursor.getString(originColumn);
    TextView origin = (TextView)view.findViewById(R.id.GrainOrigin);
    origin.setText(getOrigin);

    int lbsColumn = cursor.getColumnIndex("lbs");
    String getLbs = cursor.getString(lbsColumn);
    EditText lbs = (EditText)view.findViewById(R.id.GrainLbs);
    lbs.setText(getLbs);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = View.inflate(context, R.layout.grain_list_item, null);
    upButton = (Button)view.findViewById(R.id.UpLbsButton);
    upButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.i("Grain Adapter Action", "Button CLICKED!!!");
        }
    });
    return view;
}

【问题讨论】:

  • 问题在于布局。我希望你在行中有一些空间可以集中注意力。您可以尝试 onTouch 或单击查看视图,但我怀疑它几乎没有任何帮助。

标签: android listview focus android-edittext


【解决方案1】:

我认为你做不到。 http://www.youtube.com/watch?v=wDBM6wVEO70 该视频很长,但有关 listview 的信息很好。他们有一次解释说,这会使应用程序混淆您选择的项目。我会考虑添加另一个按钮或设置一个 longclicklistener() 并在列表视图中的所有项目之间共享它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2012-08-18
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    相关资源
    最近更新 更多