Retrieving data from EditText in Android?

心不动则不痛 提交于 2019-12-11 18:07:51

问题


How do I work with whatever the user enters into an EditText, in Java? For just one example, maybe the user enters "I had a great time this weekend!" into an EditText (like a Facebook update or Tweet). I want to do something with "I had a great time this weekend!" so how do I use Java to affect it in the app????


回答1:


You can get a value back from an EditText with

EditText editText = (EditText) findViewById(R.id.editText1);
String value = editText.getText().toString();

I recommend .trim()'ing this value, since you probably don't want the trailing whitespace users tend to leave.

You should use a Button onClick event to call the above methods, because it's more or less likely to be the UX you are looking for.




回答2:


This should work:

EditText edit = (EditText) findViewById(R.id.editTextID);
Editable editable = edit.getText();
String allTheText = editable.toString();

Replace editTextID with the ID for your EditText.




回答3:


use this code for get data from Edit text

EditText editText = (EditText) findViewById(R.id.editText1);
String value = editText.getText().toString();



回答4:


You can use this code also:

EditText edit = (EditText) findViewById(R.id.editTextID);

String string_object=String.valueOf(edit.getText());


来源:https://stackoverflow.com/questions/8498880/retrieving-data-from-edittext-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!