就像标题所说的那样我是新手。我已经学到了很多东西。 我想知道我的代码中是否有任何东西可以让我更好? 这样一个简单的应用程序似乎太过分了。 我可以巩固什么? 另外,当用户在输入数字之前按下提交时,您建议我采取什么措施。
package com.michaelpeerman.probability;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
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;
int maximum;
EditText max;
String max_string = "1000000";
int max_int = 1000000;
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.main);
submit = ((Button) findViewById(R.id.submit));
submit.setOnClickListener(this);
EditText max = (EditText) findViewById(R.id.number);
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)) {
submit();
}
return false;
}
});
}
public void onClick(View view) {
submit();
}
public void submit() {
EditText max = (EditText) findViewById(R.id.number);
maximum = Integer.parseInt(max.getText().toString());
if (maximum >= max_int) {
maximum = max_int;
max.setText(max_string);
Context context = getApplicationContext();
CharSequence text = "You entered a number greater than ten million. Setting number to ten million.";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
startThread();
} else {
startThread();
}
}
public void startThread() {
increment = 1;
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Flipping Coin...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
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);
}
}
};
}
答案 0 :(得分:0)
这个问题有点过于笼统。但对于初学者,您可能希望使用AsyncTask
作为将数据发布到UI线程的后台线程。它们更容易使用。
对于用户未写入数字的部分,向他显示对话框或Toast通知,并且不启动后台线程。