📜  如何在Android中更改输入法操作按钮?

📅  最后修改于: 2021-05-13 17:40:30             🧑  作者: Mango

在本文中,根据我们的要求,在Android中更改了IME (输入法操作)选项。输入法操作按钮位于软键盘的右下角。默认情况下,除非您的文本字段允许多行文本,否则系统将使用此按钮执行“下一步”或“完成”操作,在这种情况下,该操作按钮是回车符。但是,您可以指定可能更适合您的文本字段的其他操作,例如“转到”,“发送”,“上一步”,“无”等。“下一步”和“完成”的IME选项如下所示。

  • 下一步行动

  • 动作完成

    方法:

    1. activity_main.xml文件中添加以下代码。在此文件中,我们根据需要在EditText中指定IME选项。这里添加了一个imageview和两个edittext,并为actionDone指定了IME选项。
      activity_main.xml
      
      
        
          
        
          
        
          
        
      


      MainActivity.java
      package org.geeksforgeeks.imeoption;
        
      import androidx.appcompat.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.KeyEvent;
      import android.view.inputmethod.EditorInfo;
      import android.widget.EditText;
      import android.widget.TextView;
      import android.widget.Toast;
        
      public class MainActivity extends AppCompatActivity {
        
          @Override
          protected void onCreate(Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
        
              EditText login = findViewById(R.id.editText1);
              EditText password = findViewById(R.id.editText2);
        
              login.setOnEditorActionListener(actionListener);
              password.setOnEditorActionListener(actionListener);
          }
        
          // whenever the user clicks on IME button this listener will
          // get invoked automatically.
          private TextView.OnEditorActionListener actionListener
              = new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v,
                                                  int actionId, KeyEvent event)
                    {
                        switch (actionId) {
        
                        case EditorInfo.IME_ACTION_NEXT:
                            Toast.makeText(MainActivity.this,
                                           "Next", Toast.LENGTH_SHORT)
                                .show();
                            break;
        
                        case EditorInfo.IME_ACTION_DONE:
                            Toast.makeText(MainActivity.this,
                                           "Login", Toast.LENGTH_SHORT)
                                .show();
                            break;
                        }
                        return false;
                    }
                };
      }


    2. 现在,在MainActivity中添加以下代码。 Java文件。在此文件中,我们将listner添加到我们的EditText中,这将相应地对应于我们的操作。在这里,我们重写onEditorAction方法,并显示ACTION DONEACTION NEXT IME选项的吐司。

      主要活动。Java

      package org.geeksforgeeks.imeoption;
        
      import androidx.appcompat.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.KeyEvent;
      import android.view.inputmethod.EditorInfo;
      import android.widget.EditText;
      import android.widget.TextView;
      import android.widget.Toast;
        
      public class MainActivity extends AppCompatActivity {
        
          @Override
          protected void onCreate(Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
        
              EditText login = findViewById(R.id.editText1);
              EditText password = findViewById(R.id.editText2);
        
              login.setOnEditorActionListener(actionListener);
              password.setOnEditorActionListener(actionListener);
          }
        
          // whenever the user clicks on IME button this listener will
          // get invoked automatically.
          private TextView.OnEditorActionListener actionListener
              = new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v,
                                                  int actionId, KeyEvent event)
                    {
                        switch (actionId) {
        
                        case EditorInfo.IME_ACTION_NEXT:
                            Toast.makeText(MainActivity.this,
                                           "Next", Toast.LENGTH_SHORT)
                                .show();
                            break;
        
                        case EditorInfo.IME_ACTION_DONE:
                            Toast.makeText(MainActivity.this,
                                           "Login", Toast.LENGTH_SHORT)
                                .show();
                            break;
                        }
                        return false;
                    }
                };
      }
      
        输出: