8

誰かがソフトキーボードのエンターキーリスナーを手伝ってくれますか?

このようないくつかのeditextリスナーを内部に持つボタンリスナーのようなエンターキーリスナーが必要です

enterkey.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) { 
        if(editext1.getText().toString().equalsIgnoreCase("test1")) {
            button3.performClick();
        }
        if(editext1.getText().toString().equalsIgnoreCase("test2")) {
            button4.performClick();
        }
    }
);

このようなことが正しいかどうかも知る必要がありますか?

if(editext1.getText().toString().equals.null)) {
       testwrong.setText("Wrong"); 

このコードを使用してみましたが、Enterキーを押したときにnull値を取得し続けますか?誰かがこれを回避するための解決策を提案できますか?

editext.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            if ("test1".equalsIgnoreCase(anstext.getText().toString())) {
                but4.performClick();
            }
        } else if ("test2".equalsIgnoreCase(editext.getText().toString())) {
            but5.performClick();
        }
        if ("test5".equalsIgnoreCase(editext.getText().toString())) {
            but6.performClick();
        }
        if ("test7".equalsIgnoreCase(editext.getText().toString())) {
            but7.performClick();
        }
        if (editext.getText().toString() != null) {
            testwrong.seText("wrong");
        }
        return true;
    }
});
4

3 に答える 3

25

で、imeOptionsEditTextを使用してキーボードアクションを指定する必要があります。

<EditText
       android:id="@+id/query"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:imeOptions="actionGo"
       android:inputType="text" />

そして、あなたのアクティビティのクラスでは:

 EditText editText = (EditText) findViewById(R.id.query);
 editText.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_GO) {

                        return true;
                    }
                    return false;
                }
            });
于 2012-10-17T15:32:51.090 に答える
11

ユーザーをキャッチしたい場合EnteronKeyListenerEdittext

  yourEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode==KeyEvent.KEYCODE_ENTER) { //Whenever you got user click enter. Get text in edittext and check it equal test1. If it's true do your code in listenerevent of button3
                    if("test1".equals(edt.getText().toString())) {
                        //paste your code in button3 listener here
                        }
                }

}
    )

この部分は間違っています。

if(editext1.getText()。toString()。equals.null)){testwrong.setText( "Wrong");

に変更する必要があります

if (editext1.getText().toString() != null && !editext1.getText().toString().isEmpty()) {
  // doSomething
}
于 2012-10-17T15:32:11.007 に答える
0

これを使用して、キーボードイベントを聞くことができます

http://developer.android.com/reference/android/inputmethodservice/KeyboardView.OnKeyboardActionListener.html

于 2012-10-17T15:29:51.353 に答える