好的,在我的应用程序中,我有一个供用户输入数字的字段。我将字段设置为仅接受数字。当用户单击该字段时,它会弹出键盘。在键盘上(在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);
}
}
};
}
1
检查这个stackoverflow.com/questions/5330243/...
—
NISHANT
那是指文本框更新。
—
mpeerman'3
我想做的是也触发我已经拥有的按钮。我不想摆脱按钮,因为有些人可能正在使用没有完成按钮的键盘。
—
mpeerman'3