5

我有一个EditTextandroid希望用户输入我将存储数据库的文本。但是,在这里我无法从EditText.

这是我的代码,

EditText etUserInfoNewValue = (EditText)findViewById(R.id.etUserInfoNewVal);    
String newValue = etUserInfoNewValue.getText().toString().trim();

我怎样才能从中获得价值EditText

4

5 回答 5

8

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.

于 2012-12-27T08:32:12.477 回答
4

对我来说很好,检查你的情况..

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());
            }
        });
}
于 2012-12-27T08:35:31.673 回答
2

可能你需要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;
}

};

于 2012-12-27T09:06:26.310 回答
0

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.

于 2012-12-27T08:32:21.677 回答
0

从editText获取价值,

String getValueEditText = "null";
if (etUserInfoNewValue.getText().toString().trim().length() > 0) {
    getValueEditText = etUserInfoNewValue.getText().toString().trim();
}
于 2012-12-27T08:34:52.850 回答