2016-11-25 3 views
0

Я пытаюсь нарисовать текст редактирования программно, основываясь на моем ответе JSON. Все рисуются отлично, но я могу редактировать только последнее, а остальные недоступны для редактирования, т их фокус. Любая идея, что бы это исправить?Получение фокуса Edittext, нарисованное программно в Android

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) { 
      } 
     } 
    } 

ответ

1

Попробуйте с Linear Layout:

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"/> 
+0

Thankyou! @fsnasser Бог благословит вас.^_ ^ –

0

использование ..

editText.setFocusableInTouchMode(true); 
editText.setFocusable(true); 
editText.requestFocus(); 
Смежные вопросы