Android - 进度对话框退出时的操作
我正在尝试为我的手机编写一个非常基本的应用程序,只是为了让自己重新回到编程的状态。我已经有了我想做的事情的框架和基础知识,我只需要在一个让我痛苦的小部分上得到帮助。
在我的应用程序上,我有一个按钮。当按下该按钮时,我希望应用程序将一段文本更改为“决定...”,弹出不确定的进度条一两秒然后退出,然后打印另一个活动的结果,但仅在对话框之后已退出。我目前正在使用线程,但无法使其正常运行。
有谁知道如何让它按照我想要的方式工作?
这是我的活动文件来源:
package com.bassios.decisionengine;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class DecisionEngineActivity extends Activity {
private ProgressDialog progressDialog;
public String decision;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void decide(View view) {
TextView tv = (TextView) findViewById(R.id.Decision1);
tv.setText("Deciding...");
int a = generate();
decision = decision(a);
runDialog(1);
// alterText();
}
public void alterText() {
TextView tv = (TextView) findViewById(R.id.Decision1);
tv.setText(decision);
}
private void runDialog(final int seconds) {
progressDialog = ProgressDialog.show(this, "Please wait...", "Deciding...");
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(seconds * 1000);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public static int generate() {
int a = (int) (Math.random() * 10) + 1;
return a;
}
public static String decision(int a) {
String decision = null;
switch (a) {
case 1:
decision = "Yes";
break;
case 2:
decision = "No";
break;
case 3:
decision = "Yes";
break;
case 4:
decision = "No";
break;
case 5:
decision = "Yes";
break;
case 6:
decision = "No";
break;
case 7:
decision = "Yes";
break;
case 8:
decision = "No";
break;
case 9:
decision = "Yes";
break;
case 10:
decision = "No";
break;
}
return decision;
}
}
谢谢!
I'm trying to write a pretty basic app for my phone, just to get myself back into the swing of programming. I've got the framework and the basics of what I want to do, I just need help on a small section that's killing me.
On my app, I have a button. When that button is pressed I want the app to change a piece of text to "Deciding...", pop up an indeterminate progress bar for a second or two then exit, then print the result of another activity, but only after the dialog has exited. I'm using a thread at the moment, but I can't get it to function correctly.
Does anyone have any idea how to get it to work the way I want it to?
Here's my Activity file source:
package com.bassios.decisionengine;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class DecisionEngineActivity extends Activity {
private ProgressDialog progressDialog;
public String decision;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void decide(View view) {
TextView tv = (TextView) findViewById(R.id.Decision1);
tv.setText("Deciding...");
int a = generate();
decision = decision(a);
runDialog(1);
// alterText();
}
public void alterText() {
TextView tv = (TextView) findViewById(R.id.Decision1);
tv.setText(decision);
}
private void runDialog(final int seconds) {
progressDialog = ProgressDialog.show(this, "Please wait...", "Deciding...");
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(seconds * 1000);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public static int generate() {
int a = (int) (Math.random() * 10) + 1;
return a;
}
public static String decision(int a) {
String decision = null;
switch (a) {
case 1:
decision = "Yes";
break;
case 2:
decision = "No";
break;
case 3:
decision = "Yes";
break;
case 4:
decision = "No";
break;
case 5:
decision = "Yes";
break;
case 6:
decision = "No";
break;
case 7:
decision = "Yes";
break;
case 8:
decision = "No";
break;
case 9:
decision = "Yes";
break;
case 10:
decision = "No";
break;
}
return decision;
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用异步任务。
示例:
http://javatech.org /2011/02/discovering-android-opening-a-progress-dialog-with-asynctask/
更新:
首先,您必须像这样声明该类:
I不知道
showDialog
和removeDialog
是如何工作的,但我会这样做:显示对话框(在
onPreExecute
中):删除/关闭对话框(在
onPostExecute
中):声明三个方法中的每一个:
应该可以做到。
Use AsyncTask.
An example:
http://javatech.org/2011/02/discovering-android-opening-a-progress-dialog-with-asynctask/
Update:
First of all, you must declare the class like this:
I don't know how
showDialog
andremoveDialog
works, but I would do it like this instead:Show dialog (in
onPreExecute
):Remove/dismiss dialog (in
onPostExecute
):Declaring each of the three methods:
That should do it.