Android的使用上进行键盘按钮,点击按钮按钮、键盘、Android

2023-09-12 11:04:02 作者:︶ ̄挥霍着的青春

确定在我的应用程序,我对用户输入一个号的字段。我有字段设置为只接受数字。当用户点击该领域它带来了键盘。在键盘上(上ICS)有一个完成按钮。我想为完成按钮键盘上的触发提交按钮,我有我的应用程序。我的code是如下:

 包com.michaelpeerman.probability;

进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.DialogInterface;
进口android.content.DialogInterface.OnCancelListener;
进口android.os.Bundle;
进口android.os.Handler;
进口android.os.Message;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
导入了java.util.Random;

公共类ProbabilityActivity扩展活动实现OnClickListener {

私人按钮提交;
ProgressDialog对话框;
INT增量;
主题背景;
诠释头= 0;
诠释尾= 0;

公共无效的onCreate(包paramBundle){
    super.onCreate(paramBundle);
    的setContentView(R.layout.main);
    提交=((按钮)findViewById(R.id.submit));
    submit.setOnClickListener(本);
}

公共无效的onClick(视图查看){
    增量= 1;
    对话框=新ProgressDialog(本);
    dialog.setCancelable(真正的);
    dialog.setMessage(翻转硬币......);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);
    最大的EditText =(EditText上)findViewById(R.id.number);
    INT最大=的Integer.parseInt(max.getText()的toString());
    dialog.setMax(最大);
    dialog.show();
    dialog.setOnCancelListener(新OnCancelListener(){

          公共无效OnCancel的(DialogInterface对话){

              background.interrupt();
              TextView的结果=(TextView中)findViewById(R.id.result);
                result.setText(头:+头+\ ntails:+尾);


          }});


    背景=新主题(新的Runnable(){
        公共无效的run(){
            头= 0;
            尾= 0;
            为(诠释J = 0;!Thread.interrupted()&安培;&安培; J&其中; dialog.getMax(); J ++){
                INT I = 1 +新的随机()nextInt(2)。
                如果(ⅰ== 1)
                    头++;
                如果(ⅰ== 2)
                    尾巴++;
                progressHandler.sendMessage(progressHandler.obtainMessage());
            }
        }
    });
    background.start();
}

处理器progressHandler =新的处理程序(){
    公共无效的handleMessage(信息MSG){

        dialog.incrementProgressBy(增量);
        如果(dialog.getProgress()== dialog.getMax()){
            dialog.dismiss();
            TextView的结果=(TextView中)findViewById(R.id.result);
            result.setText(头:+头+\ ntails:+尾);


        }
    }

};

}
 

解决方案

您可以使用这其中也(设置一个特殊的监听器当上的EditText执行的动作被称为),它的作品无论是做,RETURN:

  max.setOnEditorActionListener(新OnEditorActionListener(){
        公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
            如果((事件= NULL和放大器;!及(event.getKey code()== KeyEvent.KEY code_ENTER))||(actionId == EditorInfo.IME_ACTION_DONE)){
                Log.i(TAG,输入pressed);
            }
            返回false;
        }
    });
 
android虚拟机创建打开后这样子,除了电源键点啥都没反应,6.0版本的

Ok in my app I have a field for the user to input a number. I have the field set to only accept numbers. When the user clicks on the field it brings up the keyboard. On the keyboard (on ICS) there is a done button. I would like for the done button on the keyboard to trigger the submit button i have in my application. My code is as follows.

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);


        }
    }

};

}

解决方案

You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:

max.setOnEditorActionListener(new OnEditorActionListener() {
        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;
        }
    });