点击“充值”打开“阳光卡充值”对话框,在对话框中输入充值金额,然后点击“确定”,显示“请等待”进度条。
在显示进度条的后面,创建一个线程来处理请求,当处理完毕时,不显示进度条,同时显示Toast提示充值成功。
在新建的线程里要调整UI的显示,必须通过发送请求给Handler实现。
private void credit() {
LayoutInflater factory = LayoutInflater.from(this);
// 得到自定义对话框
final View DialogView = factory.inflate(R.layout.ebcredit, null);
// 创建对话框
AlertDialog creditDialog = new AlertDialog.Builder(this)
.setTitle("阳光卡充值").setView(DialogView)// 设置自定义对话框的样式
.setPositiveButton("确定", // 设置"确定"按钮
new DialogInterface.OnClickListener(){ // 设置事件监听
public void onClick(DialogInterface dialog,
int whichButton) {
//final String amount = mEditTextCredit.getText().toString();
// 输入完成后,点击“确定”开始充值
mProgressDialog = ProgressDialog.show(
EBActivity.this, "请等待...", "正在充值...",
true);
new Thread() {
public void run() {
try {
sleep(3000);
} catch (Exception e) {
Log.e(LOG_TAG, "阳光卡充值出错", e);
} finally {
// 充值结束,取消mProgressDialog对话框
mProgressDialog.dismiss();
Message msg = new Message();
msg.what = WHAT_CREDIT;
try{
Bundle data = new Bundle();
//data.putString("amount", amount);
msg.setData(data);
handler.sendMessage(msg);
}catch(Exception ex){
Log.e(LOG_TAG, "充值出现问题", ex);
}
}
}
}.start();
}
}).setNegativeButton("取消", // 设置“取消”按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 什么都不做,点击时对话框就会关闭
}
}).create();// 创建
creditDialog.show();
}
private final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
if(msg.what==WHAT_CREDIT){
//mBalance.setText("200.50");
try{
double curBalance = Integer.parseInt(mBalance.getText().toString());
double amount = Double.parseDouble(data.getString("amount"));
double balance = curBalance + amount;
mBalance.setText(String.valueOf(balance));
DisplayToast("充值成功");
}catch(Exception ex){
Log.e(LOG_TAG, "充值出现错误", ex);
}
}
}
};
public void DisplayToast(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}