Get Value of a Edit Text field
我正在学习如何创建UI元素。 我创建了一些EditText输入字段。 在单击按钮时,我想捕获输入到该输入字段的内容。
1 | <EditText android:id="@+id/name" android:width="220px" /> |
那是我的领域。 我怎样才能获得内容?
通过使用getText():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Button mButton; EditText mEdit; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton = (Button)findViewById(R.id.button); mEdit = (EditText)findViewById(R.id.edittext); mButton.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { Log.v("EditText", mEdit.getText().toString()); } }); } |
我猜你在调用"mEdit"EditText对象时必须使用这段代码:
只需确保编译器知道要调用和使用的
从android中的
1 2 | EditText txtname = findViewById(R.id.name); String name = txtname.getText().toString(); |
我希望这个应该有效:
1 | Integer.valueOf(mEdit.getText().toString()); |
我尝试了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button rtn = (Button)findViewById(R.id.button); EditText edit_text = (EditText)findViewById(R.id.edittext1); rtn .setOnClickListener( new View.OnClickListener() { public void onClick(View view) { Log.v("EditText value=", edit_text.getText().toString()); } }); } |
您可能还想看看Butter Knife。它旨在通过使用注释减少样板代码的数量。这是一个简单的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class ExampleActivity extends ActionBarActivity { @InjectView(R.id.name) EditText nameEditText; @InjectView(R.id.email) EditText emailEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); Butterknife.inject(this); } @OnClick(R.id.submit) public void onSubmit() { Editable name = nameEditText.getText(); Editable email = emailEditText.getText(); } } |
只需将以下依赖项添加到
1 | compile 'com.jakewharton:butterknife:x.y.z' |
作为替代方案,还有AndroidAnnotations。
最短的简单
小解决方案
如果你没有,只需在你的BaseActivity / create BaseActivity中创建方法。
1 2 3 4 5 | public class BaseActivity extends AppCompatActivity{ public String getText(TextView tv) { return tv.getText().toString().trim(); } } |
并通过此BaseActivity扩展您的所有活动。
1 2 3 4 5 6 7 8 | public class YourActivity extends BaseActivity { @Override public void onCreate(Bundle savedInstanceState){ getText(editText); getText(button); getText(textView); } } |
请注意
更高级的方法是使用butterknife bindview。这消除了冗余代码。
在你的依赖关系中;添加这2行。
1 2 3 4 | compile('com.jakewharton:butterknife:8.5.1') { exclude module: 'support-compat' } apt 'com.jakewharton:butterknife-compiler:8.5.1' |
然后同步。
MainActivity中绑定edittext的示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity { @BindView(R.id.name) EditTextView mName; ... public void onCreate(Bundle savedInstanceState){ ButterKnife.bind(this); ... } } |
但是,一旦您感觉更舒服或开始使用大量数据,这是一种替代方案。
试试这个代码
1 2 3 4 5 6 7 8 9 10 | final EditText editText = findViewById(R.id.name); // your edittext id in xml Button submit = findViewById(R.id.submit_button); // your button id in xml submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String string = editText.getText().toString(); Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show(); } }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | Button kapatButon = (Button) findViewById(R.id.islemButonKapat); Button hesaplaButon = (Button) findViewById(R.id.islemButonHesapla); Button ayarlarButon = (Button) findViewById(R.id.islemButonAyarlar); final EditText ders1Vize = (EditText) findViewById(R.id.ders1Vize); final EditText ders1Final = (EditText) findViewById(R.id.ders1Final); final EditText ders1Ortalama = (EditText) findViewById(R.id.ders1Ortalama); // final EditText ders2Vize = (EditText) findViewById(R.id.ders2Vize); final EditText ders2Final = (EditText) findViewById(R.id.ders2Final); final EditText ders2Ortalama = (EditText) findViewById(R.id.ders2Ortalama); // final EditText ders3Vize = (EditText) findViewById(R.id.ders3Vize); final EditText ders3Final = (EditText) findViewById(R.id.ders3Final); final EditText ders3Ortalama = (EditText) findViewById(R.id.ders3Ortalama); // final EditText ders4Vize = (EditText) findViewById(R.id.ders4Vize); final EditText ders4Final = (EditText) findViewById(R.id.ders4Final); final EditText ders4Ortalama = (EditText) findViewById(R.id.ders4Ortalama); // final EditText ders5Vize = (EditText) findViewById(R.id.ders5Vize); final EditText ders5Final = (EditText) findViewById(R.id.ders5Final); final EditText ders5Ortalama = (EditText) findViewById(R.id.ders5Ortalama); // final EditText ders6Vize = (EditText) findViewById(R.id.ders6Vize); final EditText ders6Final = (EditText) findViewById(R.id.ders6Final); final EditText ders6Ortalama = (EditText) findViewById(R.id.ders6Ortalama); // final EditText ders7Vize = (EditText) findViewById(R.id.ders7Vize); final EditText ders7Final = (EditText) findViewById(R.id.ders7Final); final EditText ders7Ortalama = (EditText) findViewById(R.id.ders7Ortalama); // /* * * * */ kapatButon.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // kapatma islemi Toast.makeText(getApplicationContext(),"kapat", Toast.LENGTH_LONG).show(); } }); /* * * * */ hesaplaButon.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // hesap islemi int d1v = Integer.parseInt(ders1Vize.getText().toString()); int d1f = Integer.parseInt(ders1Final.getText().toString()); int ort1 = (int) (d1v * 0.4 + d1f * 0.6); ders1Ortalama.setText("" + ort1); // int d2v = Integer.parseInt(ders2Vize.getText().toString()); int d2f = Integer.parseInt(ders2Final.getText().toString()); int ort2 = (int) (d2v * 0.4 + d2f * 0.6); ders2Ortalama.setText("" + ort2); // int d3v = Integer.parseInt(ders3Vize.getText().toString()); int d3f = Integer.parseInt(ders3Final.getText().toString()); int ort3 = (int) (d3v * 0.4 + d3f * 0.6); ders3Ortalama.setText("" + ort3); // int d4v = Integer.parseInt(ders4Vize.getText().toString()); int d4f = Integer.parseInt(ders4Final.getText().toString()); int ort4 = (int) (d4v * 0.4 + d4f * 0.6); ders4Ortalama.setText("" + ort4); // int d5v = Integer.parseInt(ders5Vize.getText().toString()); int d5f = Integer.parseInt(ders5Final.getText().toString()); int ort5 = (int) (d5v * 0.4 + d5f * 0.6); ders5Ortalama.setText("" + ort5); // int d6v = Integer.parseInt(ders6Vize.getText().toString()); int d6f = Integer.parseInt(ders6Final.getText().toString()); int ort6 = (int) (d6v * 0.4 + d6f * 0.6); ders6Ortalama.setText("" + ort6); // int d7v = Integer.parseInt(ders7Vize.getText().toString()); int d7f = Integer.parseInt(ders7Final.getText().toString()); int ort7 = (int) (d7v * 0.4 + d7f * 0.6); ders7Ortalama.setText("" + ort7); // Toast.makeText(getApplicationContext(),"hesapla", Toast.LENGTH_LONG).show(); } }); |