我有一个EditText
我android
希望用户输入我将存储数据库的文本。但是,在这里我无法从EditText
.
这是我的代码,
EditText etUserInfoNewValue = (EditText)findViewById(R.id.etUserInfoNewVal);
String newValue = etUserInfoNewValue.getText().toString().trim();
我怎样才能从中获得价值EditText
?
我有一个EditText
我android
希望用户输入我将存储数据库的文本。但是,在这里我无法从EditText
.
这是我的代码,
EditText etUserInfoNewValue = (EditText)findViewById(R.id.etUserInfoNewVal);
String newValue = etUserInfoNewValue.getText().toString().trim();
我怎样才能从中获得价值EditText
?
Put
String newValue = etUserInfoNewValue.getText().toString().trim();
Inside the button's onClicklistener()
.
You have put this code in onCreate() just after declaring it, it won't have any value at all. So you will get null value.
对我来说很好,检查你的情况..
Button buttonTest;
EditText editText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonTest = (Button)findViewById(R.id.button);
editText = (EditText)findViewById(R.id.edittext); //check this point carefully on your program
buttonTest.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText..", editText.getText().toString().trim());
}
});
}
可能你需要TextView.OnEditorActionListener。蓝牙聊天 http://developer.android.com/tools/samples/index.html
//BluetoothChat.java:
// Initialize the compose field with a listener for the return key
EditText mOutEditText = (EditText) findViewById(R.id.edit_text_out);
mOutEditText.setOnEditorActionListener(mWriteListener);
// The action listener for the EditText widget, to listen for the return key
private TextView.OnEditorActionListener mWriteListener =
new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
// If the action is a key-up event on the return key, send the message
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
String message = view.getText().toString();
sendMessage(message);
}
if(D) Log.i(TAG, "END onEditorAction");
return true;
}
};
When you will store the EditText String to Database. By seeing at your code it looks that you are immediately storing the Text in String newValue and then storing it in the Database. Change the code of String declaration to onClick() of your button or anything else.
从editText获取价值,
String getValueEditText = "null";
if (etUserInfoNewValue.getText().toString().trim().length() > 0) {
getValueEditText = etUserInfoNewValue.getText().toString().trim();
}