activity_main.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingEnd="16dp" android:paddingStart="16dp" android:paddingTop="200dp" tools:context="com.codinginflow.imeoptionsexample.MainActivity"> <EditText android:id="@+id/edit_text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" android:inputType="text" /> <EditText android:id="@+id/edit_text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:imeOptions="actionSend" android:inputType="textPassword" /> </LinearLayout>
package com.codinginflow.imeoptionsexample; import android.support.v7.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 editText1 = findViewById(R.id.edit_text1); EditText editText2 = findViewById(R.id.edit_text2); editText1.setOnEditorActionListener(editorListener); editText2.setOnEditorActionListener(editorListener); } private TextView.OnEditorActionListener editorListener = 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_SEND: Toast.makeText(MainActivity.this, "Send", Toast.LENGTH_SHORT).show(); break; } return false; } }; }