2014-05-10 65 views
0

我根據項目需求在佈局中動態創建視圖。視圖序列來自json。當我點擊編輯文本,然後timepicker函數要求在編輯文本中設置時間,但是當我設置編輯文本值時,它會給出最後創建的編輯文本的ID。 這裏是我的代碼: -如何在android中動態創建編輯文本時點擊編輯文本的ID?

for (i = 0; i < alist_interface_type.size(); i++) { 
       if(alist_interface_type.get(i).equals("textbox")) { 
         LinearLayout lLayout = new LinearLayout(getActivity()); 
         lLayout.setOrientation(LinearLayout.VERTICAL); 
         lLayout.setPadding(5, 5, 5, 5); 
         lLayout.setBackgroundColor(Color.parseColor("#E0E0E0")); 
         label = new TextView(getActivity()); 
         label.setText(example.get(i)); 
         label.setTextSize(16); 
         label.setTextColor((Color.parseColor("#01c534"))); 
         label.setPadding(5, 5, 5, 5); 
         et = new EditText(getActivity()); 
         et.setSingleLine(true); 
         et.setId(i); 
         et.setTextSize(18); 
         et.setPadding(5, 5, 5, 5); 
         if (!data_type.get(i).toString().equals("text")) { 
          InputFilter[] filters = new InputFilter[1]; 
          filters[0] = new InputFilter.LengthFilter(Integer.parseInt(data_type.get(i).toString())); 
          et.setFilters(filters); 
         } 
         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
           LayoutParams.MATCH_PARENT, 
           LayoutParams.WRAP_CONTENT); 
         lLayout.addView(label, lp); 
         lLayout.addView(et, lp); 
         ll.addView(lLayout); 
         lp.setMargins(5, 0, 5, 5); 
         lLayout.setLayoutParams(lp); 
         alist_id.add(i); 
         alist_views.add("EditText"); 
        } 
       else if (alist_interface_type.get(i).equals("time") { 
        LinearLayout lLayout = new LinearLayout(getActivity()); 
        lLayout.setOrientation(LinearLayout.VERTICAL); 
        lLayout.setPadding(5, 5, 5, 5); 
        lLayout.setBackgroundColor(Color.parseColor("#E0E0E0")); 
        label = new TextView(getActivity()); 
        label.setText(example.get(i)); 
        label.setTextSize(16); 
        label.setTextColor((Color.parseColor("#01c534"))); 
        label.setPadding(5, 5, 5, 5); 
        et2 = new EditText(getActivity()) 
        { 
         public boolean getDefaultEditable() { 
          return false; 
         } 
        }; 
        et2.setSingleLine(true); 
        et2.setId(i); 
        et2.setTextSize(18); 
        Log.d("edit text cliked", ""+i); 
        et2.setText(pref.getString(alist_Label_name.get(i),"")); 
        et2.setPadding(5, 5, 5, 5); 
        if (!data_type.get(i).toString().equals("text")) { 
         InputFilter[] filters = new InputFilter[1]; 
         filters[0] = new InputFilter.LengthFilter(Integer.parseInt(data_type.get(i).toString())); 
         et2.setFilters(filters); 
        } 
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
          LayoutParams.MATCH_PARENT, 
          LayoutParams.WRAP_CONTENT); 
        lLayout.addView(label, lp); 
        lLayout.addView(et2, lp); 
        lp.setMargins(5, 0, 5, 5); 
        lLayout.setLayoutParams(lp); 
        ll.addView(lLayout); 
        alist_id.add(i); 
        alist_views.add("time"); 
        et2.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          // TODO Auto-generated method stub 

          Log.d("id of edit text", ""+et2.getId()); 
          timePicker(); 
         } 
        }); 
       } 

        } 

在這段代碼alist_interface_type是數組列表,包含我的views.If這個列表中包含「時間」,然後創建編輯文字,然後點擊編輯文本通話時間選擇方法選擇的時間和設置編輯文本。我給我的價值編輯文本的ID。單擊編輯文本它不會給特定點擊編輯文本的ID設置值。 enter image description here

+0

使用setTag()時,時間選擇聽者回選定的時間和你這段時間設置爲您的EditText, –

+0

我認爲你有一個監聽器爲您timepicker()一次選擇@BirajZalavadia它不是一個列表視圖。我在佈局中動態創建視圖。 –

+0

方法來識別領域 – Achin

回答

1
// try this way,hope this will help you... 

et2.setTag(et2.getId()); 
et2.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(final View v) { 
     // TODO Auto-generated method stub 

     Log.d("id of edit text", ""+et2.getId()); 
     timePicker(new OnTimeSelected(){ 
      public void onSelect(String time){ 
       ((EditText)v.getTag()).setText(time); 
      } 
     }); 
    } 
}); 


interface OnTimeSelected{ 
    public void onSelect(String time); 
}