5

EditTextユーザーにテキストを入力しandroidてもらいたいものがあります。データベースを保存します。しかし、ここでは から値を取得できませんEditText

ここに私のコードがあります、

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

これから値を取得するにはどうすればよいEditTextですか?

4

5 に答える 5

8

置く

String newValue = etUserInfoNewValue.getText().toString().trim(); 

ボタンのonClicklistener().

このコードを宣言した直後に onCreate() に入れましたが、値はまったくありません。したがって、null 値が取得されます。

于 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が必要な場合があります。BluetoothChat 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

EditText 文字列をデータベースに保存するとき。コードを見ると、テキストをすぐに String newValue に格納してからデータベースに格納しているように見えます。String 宣言のコードをボタンの onClick() などに変更します。

于 2012-12-27T08:32:21.677 に答える