在Android中以编程方式绘制Edittext的焦点

时间:2016-11-25 10:41:12

标签: android json android-edittext

我正在尝试以编程方式绘制编辑文本,基于我的JSON响应。所有绘制完美,但我只能编辑最后一个而其他的不可编辑,我无法得到他们的关注。知道什么会解决这个问题吗?

  RelativeLayout layout =(RelativeLayout)getActivity().findViewById(R.id.layout_items);
        JSONArray result=(JSONArray)o;
        for (int i = 0; i < result.length(); i++) {
            try {
                relativeLayoutParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

                 editText = new EditText(context);
                editText.setHint(result.getJSONObject(i).getString(config.TAG_NAME));
                editText.setPadding(0,20,0,0);
                editText.setId(Integer.parseInt(result.getJSONObject(i).getString(config.TAG_ID)));
                editText.setEnabled(true);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
                relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, layout.getId());
                layout.addView(editText, relativeLayoutParams);
             }
             catch (Exception ex) {
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

尝试线性布局:

    LinearLayout layout = (LinearLayout) getActivity().findViewById(R.id.layout_items);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    JSONArray result = (JSONArray) o;
    for (int i = 0; i < result.length(); i++) {
        EditText editText = new EditText(context);
        editText.setHint(result.getJSONObject(i).getString(config.TAG_NAME));
        editText.setPadding(0,i*100,0,0);
        editText.setId(Integer.parseInt(result.getJSONObject(i).getString(config.TAG_ID)));
        editText.setEnabled(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.setLayoutParams(layoutParams);
        layout.addView(editText);
    }

在你的布局xml:

<LinearLayout
    android:id="@+id/layout_items"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>

答案 1 :(得分:0)

使用..

editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
editText.requestFocus();