2014-10-01 73 views
0

我正尝试以编程方式在窗体中创建电话字段。如何识别并随后获得其价值?如何以编程方式创建EditText,然后获取其值

,创造了EditText领域,并添加到窗体中的代码是这样的:

public void addTelephone(){   
      //form 
      TableLayout table = (TableLayout)findViewById(R.id.table); 
      //new row 
      TableRow tr = new TableRow(NewRequestActivity.this); 

      LayoutParams ltr = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
      ltr.setMargins(0, 0, 0, 2500); 
      tr.setLayoutParams(ltr); 
      //new field 
      EditText et = new EditText(NewRequestActivity.this); 
      et.setHint("telephone"); 
      et.setInputType(InputType.TYPE_CLASS_NUMBER);  
      et.requestFocus(); 
      Resources res = getResources(); 
      float fontSize = res.getDimension(R.dimen.input_form)/getResources().getDisplayMetrics().density; 
      et.setTextSize(fontSize); 

      LayoutParams let = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1); 

      et.setLayoutParams(let); 

      ImageView img = new ImageView(NewRequestActivity.this); 
      img.setTag("img_"+counttelf); 
      img.setImageResource(R.drawable.more); 
      img.setBackgroundResource(R.drawable.imagefocused); 
      img.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) {    

         v.setVisibility(View.GONE); 
         addTelephone(); 
       } 
      }); 
      LayoutParams limg = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      img.setLayoutParams(limg); 

      tr.addView(et, 0); 
      tr.addView(img, 1); 
table.addView(tr, ++indextelf); 
    } 
+0

检查:http://stackoverflow.com/questions/23577420/how-to-get-id-of-edit-text-on-click-of-dynamically-created -edit-text-in-android/23577508#23577508 – 2014-10-01 07:07:04

回答

0

集ID给每个动态创建的编辑文本和参照它们的ID获取值。 要设置ID:

edittext.setId(i); 

从标识获取:

int i = edittext.getId(); 
1

你可以setTag您的EditText

如:

你有手机领域:

editText.setTag("phone"); 

当你想检索它。

只要做,

EditText ed = (EditText)findViewByTag("phone"); 
String text = ed.getText().toString(); 
+0

什么是视图变量? – Hanzo 2014-10-01 07:47:22

+0

我想用'“telephone”+ counter'标签获得EditText的值,当点击“发送”按钮时 – Hanzo 2014-10-01 07:55:26

+0

您可以根据逻辑修改我的答案,并且view只是editText或TextView之类的东西。在你的情况下,它是editText – 2014-10-01 08:01:24

相关问题