ListView中的第一个EditText自动请求焦点

时间:2016-02-05 16:02:28

标签: android listview android-edittext focus lost-focus

我的ListView包含4 EditText

每次用户输入或点击TextView(不是列表中的第一个)时:

  • 第一个TextView获得焦点

  • 点击的TextView或用户输入的TextView获得焦点

有时这个循环重复2到3次。

这是onFocusChange方法的堆栈跟踪:

at it.softecspa.workflowmanager.views.IntegerNumber_Type.onFocusChange(IntegerNumber_Type.java:308)
  at android.view.View.onFocusChanged(View.java:5592)
  at android.widget.TextView.onFocusChanged(TextView.java:8855)
  at android.widget.EditText.onFocusChanged(EditText.java:182)
  at android.view.View.clearFocusInternal(View.java:5475)
  at android.view.View.unFocus(View.java:5508)
  at android.view.ViewGroup.unFocus(ViewGroup.java:982)
  at android.view.ViewGroup.unFocus(ViewGroup.java:982)
  at android.view.ViewGroup.requestChildFocus(ViewGroup.java:672)
  at android.view.ViewGroup.requestChildFocus(ViewGroup.java:678)
  at android.view.ViewGroup.requestChildFocus(ViewGroup.java:678)
  at android.view.View.handleFocusGainInternal(View.java:5340)
  at android.view.View.requestFocusNoSearch(View.java:8080)
  at android.view.View.requestFocus(View.java:8059)
  at android.view.View.requestFocus(View.java:8026)
  at android.view.View.requestFocus(View.java:8005)
  at android.widget.ListView.layoutChildren(ListView.java:1766)
  at android.widget.AbsListView.onLayout(AbsListView.java:2632)
  at android.view.View.layout(View.java:16550)
  at android.view.ViewGroup.layout(ViewGroup.java:5303)
  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
  at android.view.View.layout(View.java:16550)
  at android.view.ViewGroup.layout(ViewGroup.java:5303)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
  at android.view.View.layout(View.java:16550)
  at android.view.ViewGroup.layout(ViewGroup.java:5303)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
  at android.view.View.layout(View.java:16550)
  at android.view.ViewGroup.layout(ViewGroup.java:5303)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
  at android.view.View.layout(View.java:16550)
  at android.view.ViewGroup.layout(ViewGroup.java:5303)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
  at android.view.View.layout(View.java:16550)
  at android.view.ViewGroup.layout(ViewGroup.java:5303)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
  at android.view.View.layout(View.java:16550)
  at android.view.ViewGroup.layout(ViewGroup.java:5303)
  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2308)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2021)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
  at android.view.Choreographer.doCallbacks(Choreographer.java:590)
  at android.view.Choreographer.doFrame(Choreographer.java:560)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5832)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

1 个答案:

答案 0 :(得分:0)

我想我需要查看更多代码(xml和启动listview的代码)。同样,我在视图中保持焦点时遇到了问题。 这是一个工作正常并且不使用OnFocusChange()

的示例
public class MainActivity extends AppCompatActivity {

MyAdapter.ViewHolder holder;
MyAdapter adapt;
List<String> dataArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    populateList();
}
private void populateList() {
    dataArray = new ArrayList<String>();

    ListView listView = (ListView) findViewById(android.R.id.list);

    dataArray.add("Item 1");
    dataArray.add("Item 2");
    dataArray.add("Item 3");
    dataArray.add("Item 4");

    adapt = new MyAdapter(this, R.layout.list_rows, dataArray);

    listView.setAdapter(adapt);

}

private class MyAdapter extends ArrayAdapter<String> {

    Context context;
    List<String> itemList = new ArrayList<String>();
    int layoutResourceId;

    public MyAdapter(Context context, int layoutResourceId,
                     List<String> objects) {
        super(context, layoutResourceId, objects);
        this.layoutResourceId = layoutResourceId;
        this.itemList = objects;
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        holder = null;

        if (convertView == null) {

            //inflate layout
            convertView = inflater.inflate(R.layout.list_rows, parent, false);

            //create view holder
            holder = new ViewHolder();

            //find views
            holder.et1 = (EditText) convertView.findViewById(R.id.et_1);
            holder.et2 = (EditText) convertView.findViewById(R.id.et_2);
            holder.et3 = (EditText) convertView.findViewById(R.id.et_3);
            holder.et4 = (EditText) convertView.findViewById(R.id.et_4);

            //attach the views to the holder
            convertView.setTag(holder);


        } else {
            //return the views if the converter is not null
            holder = (ViewHolder) convertView.getTag();

        }

        return convertView;
    }

    class ViewHolder {
        EditText et1;
        EditText et2;
        EditText et3;
        EditText et4;
    }

}

如果您需要将焦点保持在一个EditText,尤其是holder.et1.requestFocus();

我希望这有帮助,如果不是,我会等你的代码(;