Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以下代码不是第一次显示toast消息_Android - Fatal编程技术网

Android 以下代码不是第一次显示toast消息

Android 以下代码不是第一次显示toast消息,android,Android,这是代码 package com.Wase.edittext; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; impo

这是代码

package com.Wase.edittext;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
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(keyCode == EditorInfo.IME_ACTION_GO)
        {
            edittext1.requestFocus();
            return true;
        }

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

        // if keydown and "enter" is pressed
             if(b == EditorInfo.IME_ACTION_GO) {

            //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().toString() + " " + edittext1.getText().toString(), Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
} });
        return false;
}
    });
 }
}
此代码不会第一次显示toast消息。用户必须返回到第一个编辑文本。重新键入文本。返回到第二个编辑文本并重新键入,然后必须单击go以显示祝酒词

请帮助解决

像这样尝试

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(keyCode == EditorInfo.IME_ACTION_GO)
            {
                edittext1.requestFocus();
                return true;
            }


            return false;
         } });

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

            // if keydown and "enter" is pressed
                 if(b == EditorInfo.IME_ACTION_GO) {

                //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().toString() + " " + edittext1.getText().toString(), Toast.LENGTH_SHORT).show();
                return true;
            }

            return false;
          } });

     }

您的edittext.setOnEditorActionListener方法的括号--}--关闭错误行。

如果使用edittext.requestFocus(),为什么在edittext-One中有edittext1
OnEditorActionListener
;要打开toast,您可以尝试edittext.performClick();也许这样行。你能指出行号吗?它不会有帮助,因为我需要在单击Go virtual keyboard按钮后显示。如果我尝试此操作,对象edittext中的值将一直存在,直到toast是endedIt,这对于私有类型不重要。问题是,据我所知,您在edittext setOnEditorActionListener中为edittext1定义了OnEditorActionListener。
  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 (keyCode == EditorInfo.IME_ACTION_GO) {

          edittext1.requestFocus();
          return true;
        } else {
          return false;
        }
      }
    });
    edittext1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView a, int b, KeyEvent c) {


        // if keydown and "enter" is pressed
        if (b == EditorInfo.IME_ACTION_GO) {

          // hide the keyboard
          InputMethodManager imm =
              (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
          imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);


          Toast.makeText(MainActivity.this,
              edittext.getText().toString() + " " + edittext1.getText().toString(),
              Toast.LENGTH_LONG).show();


          return true;
        } else {

          return false;
        }
      }
    });


  }