我想在点击虚拟键盘上的输入键后显示吐司信息

时间:2013-12-23 16:02:20

标签: android android-virtual-keyboard

请在下方找到更新后的代码。

以下是代码:

package com.Wase.edittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;
import com.Wase.edittext.R;
import android.widget.TextView;

public class MyAndroidAppActivity extends Activity {

 private EditText edittext;
 private EditText edittext1;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_android_app);

    addKeyListener();
 edittext.requestFocus();
}



 public void addKeyListener() {

    // get edittext component
    edittext = (EditText) findViewById(R.id.editText);
    edittext1 = (EditText) findViewById(R.id.editText1);



    // add a keylistener to keep track user input
    edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {

    // if keydown and "enter" is pressed
    if ((event.getAction() == KeyEvent.ACTION_DOWN)
        && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
    {
        edittext1.requestFocus();
    }

    edittext1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
         @Override
         public boolean onEditorAction(TextView a, int b, KeyEvent c) {

    // if keydown and "enter" is pressed
    if ((c.getAction() == KeyEvent.ACTION_DOWN)
        && (c.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

        //hide the keyboard
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
    // display a floating message
        Toast.makeText(MyAndroidAppActivity.this,
            edittext.getText() + " " + edittext1.getText(), Toast.LENGTH_SHORT).show();
        return true;
    }

        return false;
}
 });
        return false;
}
    });
 }
}

此代码仅适用于键盘点击或系统键盘点击。我想隐藏虚拟键盘并在点击android虚拟键盘的输入键时显示toast消息。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

每次在edittext中按下某个键时,您都要为edittext1设置一个新的onKeyListener ...实际上您想要做什么?

按下回车键时你必须检测的代码似乎很好。您需要添加以下代码才能关闭键盘并进行干杯。

editText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
            {
                InputMethodManager imm = (InputMethodManager)getSystemService(
                    Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
                Toast.makeText(MyAndroidAppActivity.this, "My message", Toast.LENGTH_SHORT).show();
            }
       }
}