键盘上的Android使用完成按钮单击按钮

mpe*_*man 116 android keyboard-events android-softkeyboard

在我的应用程序中确定我有一个字段供用户输入数字.我将字段设置为仅接受数字.当用户点击该字段时,它会调出键盘.在键盘上(在ICS上)有一个完成按钮.我想让键盘上的完成按钮触发我在我的应用程序中的提交按钮.我的代码如下.

package com.michaelpeerman.probability;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class ProbabilityActivity extends Activity implements OnClickListener {

private Button submit;
ProgressDialog dialog;
int increment;
Thread background;
int heads = 0;
int tails = 0;

public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    setContentView(R.layout.main);
    submit = ((Button) findViewById(R.id.submit));
    submit.setOnClickListener(this);
}

public void onClick(View view) {
    increment = 1;
    dialog = new ProgressDialog(this);
    dialog.setCancelable(true);
    dialog.setMessage("Flipping Coin...");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);
    EditText max = (EditText) findViewById(R.id.number);
    int maximum = Integer.parseInt(max.getText().toString());
    dialog.setMax(maximum);
    dialog.show();
    dialog.setOnCancelListener(new OnCancelListener(){

          public void onCancel(DialogInterface dialog) {

              background.interrupt();
              TextView result = (TextView) findViewById(R.id.result);
                result.setText("heads : " + heads + "\ntails : " + tails);


          }});


    background = new Thread(new Runnable() {
        public void run() {
            heads=0;
            tails=0;
            for (int j = 0; !Thread.interrupted() && j < dialog.getMax(); j++) {
                int i = 1 + new Random().nextInt(2);
                if (i == 1)
                    heads++;
                if (i == 2)
                    tails++;
                progressHandler.sendMessage(progressHandler.obtainMessage());
            }
        }
    });
    background.start();
}

Handler progressHandler = new Handler() {
    public void handleMessage(Message msg) {

        dialog.incrementProgressBy(increment);
        if (dialog.getProgress() == dialog.getMax()) {
            dialog.dismiss();
            TextView result = (TextView) findViewById(R.id.result);
            result.setText("heads : " + heads + "\ntails : " + tails);


        }
    }

};

}
Run Code Online (Sandbox Code Playgroud)

vla*_*ija 292

你也可以使用这个(在EditText上执行一个动作时设置一个特殊的监听器),它既适用于DONE,也适用于RETURN:

max.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                Log.i(TAG,"Enter pressed");
            }    
            return false;
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 当按下DONE按钮时,这对我不起作用,KeyEvent始终为null ..但是actionId设置为EditorInfo.IME_ACTION_DONE,我可以使用它.(这是在android 4.2.2中,此时甚至不匹配android sdk文档) (3认同)
  • 您可以将所有代码移动到单个函数中,然后调用它或使用[performClick()](http://developer.android.com/reference/android/view/View.html#performClick%28%29) (2认同)

Gib*_*olt 22

科特林解决方案

在 Kotlin 中处理 done 动作的基本方法是:

edittext.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Call your code here
        true
    }
    false
}
Run Code Online (Sandbox Code Playgroud)

Kotlin 扩展

使用它来调用edittext.onDone {/*action*/}您的主代码。使其更具可读性和可维护性

edittext.onDone { submitForm() }

fun EditText.onDone(callback: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            callback.invoke()
            true
        }
        false
    }
}
Run Code Online (Sandbox Code Playgroud)

不要忘记将这些选项添加到您的编辑文本中

<EditText ...
    android:imeOptions="actionDone"
    android:inputType="text"/>
Run Code Online (Sandbox Code Playgroud)

如果您需要inputType="textMultiLine"支持,请阅读这篇文章


Int*_*iya 21

你可以试试IME_ACTION_DONE.

此操作执行"完成"操作,无需输入任何内容,IME将关闭.

 Your_EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                  /* Write your logic here that will be executed when user taps next button */


                    handled = true;
                }
                return handled;
            }
        });
Run Code Online (Sandbox Code Playgroud)

  • 这个适合我.请注意返回TRUE.键盘仍然显示.如果要隐藏键盘返回FALSE. (6认同)

Rom*_*ack 16

试试这个:

max.setOnKeyListener(new OnKeyListener(){
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event){
        if(keyCode == event.KEYCODE_ENTER){
            //do what you want
        }
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 8

试试这个Xamarin.Android(跨平台)

edittext.EditorAction += (object sender, TextView.EditorActionEventArgs e) {
       if (e.ActionId.Equals (global::Android.Views.InputMethods.ImeAction.Done)) {
           //TODO Something
       }
};
Run Code Online (Sandbox Code Playgroud)