哪位大神看看我的对话框为什么只能用一次,再用就崩了。

CS科研GO 2017-09-28 05:31:16
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();
}

}

...全文
525 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
a25475869jk 2017-09-30
  • 打赏
  • 举报
回复
啦啦啦啦啦啦啊啦啦啦啦啦啦啊
CS科研GO 2017-09-30
  • 打赏
  • 举报
回复
已结贴
devin_jia 2017-09-29
  • 打赏
  • 举报
回复
那不是可以蹭分
CS科研GO 2017-09-29
  • 打赏
  • 举报
回复
啦啦啦啦啦啦啊自己已解决

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧