哪位大神看看我的对话框为什么只能用一次,再用就崩了。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
int flag=1;
private int minute = 05;//这是分钟
private int second = 00;//这是分钟后面的秒数。这里是以30分钟为例的,所以,minute是30,second是0
private TextView timeView;
private Button time;
private Button dialog;
private Timer timer;
private TimerTask timerTask;
//这是接收回来处理的消息
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (minute == 0) {
if (second == 0) {
time.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
} else {
second--;
if (second >= 10) {
time.setText("0" + minute + ":" + second);
} else {
time.setText("0" + minute + ":0" + second);
}
}
} else {
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {
time.setText(minute + ":" + second);
} else {
time.setText("0" + minute + ":" + second);
}
} else {
second--;
if (second >= 10) {
if (minute >= 10) {
time.setText(minute + ":" + second);
} else {
time.setText("0" + minute + ":" + second);
}
} else {
if (minute >= 10) {
time.setText(minute + ":0" + second);
} else {
time.setText("0" + minute + ":0" + second);
}
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time =(Button) findViewById(R.id.button1);
timerTask = new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
};
timer = new Timer();
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.timer,
(ViewGroup) findViewById(R.id.dialog));
final AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this)
.setTitle("设置时间")
.setView(null)
.setView(layout)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
EditText v_minute = (EditText) layout
.findViewById(R.id.et_minute);
EditText v_second = (EditText) layout
.findViewById(R.id.et_second);
String minu = v_minute.getText().toString();
String seco = v_second.getText().toString();
if(TextUtils.isEmpty(minu)&&TextUtils.isEmpty(seco)){
Toast.makeText(MainActivity.this, "您输入的时间有误,请重新输入!!!",
Toast.LENGTH_LONG).show();
}else {
minute = Integer.parseInt(v_minute.getText().toString());
second = Integer.parseInt(v_second.getText().toString());
if(minute<61&&minute>=0&&second<61&&second>=0){
timer.schedule(timerTask, 0, 1000);
} else {
Toast.makeText(MainActivity.this, "您输入的时间有误,请重新输入!!!",
Toast.LENGTH_LONG).show();
}
}
}
})
.setNegativeButton("取消", null);
AlertDialog dialog1 = builder1.create();
dialog1.show();
}
});
}
@Override
protected void onDestroy() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
minute = -1;
second = -1;
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onPause() {
super.onPause();
}
}
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
int flag=1;
private int minute = 05;//这是分钟
private int second = 00;//这是分钟后面的秒数。这里是以30分钟为例的,所以,minute是30,second是0
private TextView timeView;
private Button time;
private Button dialog;
private Timer timer;
private TimerTask timerTask;
//这是接收回来处理的消息
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (minute == 0) {
if (second == 0) {
time.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
} else {
second--;
if (second >= 10) {
time.setText("0" + minute + ":" + second);
} else {
time.setText("0" + minute + ":0" + second);
}
}
} else {
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {
time.setText(minute + ":" + second);
} else {
time.setText("0" + minute + ":" + second);
}
} else {
second--;
if (second >= 10) {
if (minute >= 10) {
time.setText(minute + ":" + second);
} else {
time.setText("0" + minute + ":" + second);
}
} else {
if (minute >= 10) {
time.setText(minute + ":0" + second);
} else {
time.setText("0" + minute + ":0" + second);
}
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time =(Button) findViewById(R.id.button1);
timerTask = new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
};
timer = new Timer();
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.timer,
(ViewGroup) findViewById(R.id.dialog));
final AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this)
.setTitle("设置时间")
.setView(null)
.setView(layout)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
EditText v_minute = (EditText) layout
.findViewById(R.id.et_minute);
EditText v_second = (EditText) layout
.findViewById(R.id.et_second);
String minu = v_minute.getText().toString();
String seco = v_second.getText().toString();
if(TextUtils.isEmpty(minu)&&TextUtils.isEmpty(seco)){
Toast.makeText(MainActivity.this, "您输入的时间有误,请重新输入!!!",
Toast.LENGTH_LONG).show();
}else {
minute = Integer.parseInt(v_minute.getText().toString());
second = Integer.parseInt(v_second.getText().toString());
if(minute<61&&minute>=0&&second<61&&second>=0){
timer.schedule(timerTask, 0, 1000);
} else {
Toast.makeText(MainActivity.this, "您输入的时间有误,请重新输入!!!",
Toast.LENGTH_LONG).show();
}
}
}
})
.setNegativeButton("取消", null);
AlertDialog dialog1 = builder1.create();
dialog1.show();
}
});
}
@Override
protected void onDestroy() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
minute = -1;
second = -1;
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onPause() {
super.onPause();
}
}
...全文
相关推荐
jQuery ui1.7 dialog只能弹出一次问题
"jQuery UI 1.7 弹出对话框只能显示一次的问题及解决方案"在使用jQuery UI 1.7版本时,开发者可能会遇到一个常见的问题:对话框(dialog)只能弹出显示一次。这个问题通常
浅谈request.getinputstream只能读取一次的问题
然而,一个重要的概念是,`getInputStream()`返回的`ServletInputStream`只能被读取一次,这是由其内部实现机制决定的。
JS实现网页每隔3秒弹出一次对话框的方法
不过在上述示例代码中,由于没有保存`setInterval()`返回的定时器ID,因此没有直接停止对话框弹出的方法。上述方法仅需短短几行代码,就能实现网页每隔3秒弹出一次对话框的需求。
完美解决request请求流只能读取一次的问题
完美解决request请求流只能读取一次的问题在Web开发中,Request对象的InputStream只能读取一次,这是一个常见的限制。
OkHttp踩坑随笔为何 response.body().string() 只能调用一次
"OkHttp使用过程中遇到的问题及源码解析:为什么response.body().string()只能调用一次"在OkHttp的使用中,开发者可能会遇到一个常见的陷阱:不能多次调用`respo


