2016-11-25 8 views
0

私はJSONレスポンスに基づいてプログラムでテキストを描画しようとしています。完全に描画されますが、最後のもののみを編集でき、彼らの焦点を得る。何がこれを修正するだろう任意のアイデア?Androidでプログラマチックに描画されたEdittextのフォーカスを取得

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

リニアレイアウトを試してみてください:

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

ありがとう! @fsnasser神はあなたを祝福します。^_ ^ –

0

使用..

editText.setFocusableInTouchMode(true); 
editText.setFocusable(true); 
editText.requestFocus(); 
関連する問題