- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
下面是我的代码,我试图通过 onProgressUpdate 方法在 Asyntask 中显示我的进度,但它没有显示在警报对话框中。仅显示初始消息。
class DownloadFileFromURL extends AsyncTask<String, String, String> {
private AlertDialog.Builder alert;
private int progress = 0;
/**
* Before starting background thread Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
alert = new AlertDialog.Builder(context);
alert.setTitle("Downloading..");
alert.setMessage("1");
alert.setCancelable(false);
alert.show();
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100%
// progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
*/
@Override
protected void onProgressUpdate(String... progress) {
Log.d("Myapp","progress :"+progress[0]);
alert.setMessage(""+progress[0]);
}
/**
* After completing background task Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
}
}
因为我还在 onProgressUpdate 中写了一个日志来显示进度更新,所以打印了日志,但是 onProgressUpdate 中的 alert.setMessage 似乎没有将消息设置到我的警报对话框。
最佳答案
根据您的代码,alert
是一个 AlertDialog.Builder
而不是 AlertDialog
本身。这引起了我的关注,因为它可能不会更改的原因是因为您已经显示了构建器,但没有提供 AlertDialog
。所以我尝试了一个简单的代码:
public class MainActivity extends AppCompatActivity {
private AlertDialog.Builder alert;
private AlertDialog ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alert = new AlertDialog.Builder(this);
alert.setTitle("Downloading..");
alert.setMessage("1");
alert.setCancelable(false);
ad = alert.show();
Log.d("SAMPLE", "SET MESSAGE 2");
alert.setMessage("2");
Log.d("SAMPLE", "SET MESSAGE 3");
ad.setMessage("3");
}
}
起初,我只是使用了alert.setMessage
(这是AlertDialog.Builder
),消息根本没有改变。但是放在一个AlertDialog
中,然后设置AlertDialog
实例的消息后,消息就变了。小心尝试这种方法。首先将 AlertDialog.Builder
传递给 AlertDialog
,然后 setMessage
使用 AlertDialog
实例。
AlertDialog 的文档和 AlertDialog.Builder .
关于android - AlertDialog setmessage 在 Asynctask 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019457/
我正在尝试在 AlertDialog 中创建一个 AlertDialog,但是当我运行代码时,没有出现第二个 AlertDialog 这是我的代码,我想让它像如果用户在第一个 AlertDialog
相关问题是here 制作android应用,我觉得我的代码不酷。 因为,每当需要对话框时我都会创建新的 AlderDialog.Builder 以防止出现此错误 “指定的子项已有父项。您必须先对子项的
我正在使用以下代码来创建警报对话框。 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(thi
我正在尝试在警报对话框中添加一个警报对话框。但是看不到第二个警报对话框。请帮助我这是我显示的代码 AlertDialog alertDialog = new AlertDialog.Builder(m
为什么要使用 AlertDialog.Builder 类而不是 AlertDialog 直接可用的方法,例如,为什么使用 AlertDialog.Builder.setCancellable 而不是
假设您有一个带有两个按钮 A 和 B 的 AlertDialog。我发现在某些设备和某些版本的 android 上,如果您触摸对话框周围屏幕的任何区域,AlertDialog 消失。在其他设备上,您被
我目前正在使用需要使用大量 AlertDialogs 的应用程序。我目前在这里编写了一个基本的代码: protected void StopButton () { AlertDialog.Bu
我正在开发一个 Android 应用程序,并且我有一个 AlertDialog 子类。我想在对话框标题区域的右侧放置 2 个 ImageButtons(类似于 Activity 中的 ActionBa
我一直在尝试在 AlertDialog 中制作一个按钮Flutter 中的盒子。但我找不到拉伸(stretch)按钮容器的方法。请检查我的代码并查看下面的示例图片。 AlertDialog(
我正在尝试找出创建对话框的最佳方式。我可以创建自己的 Dialog 类(对我来说,它更干净、更有条理),或者我可以使用 AlertDialog.Builder(可以内联完成,而且看起来很时髦)……两者
我使用 AlertDialog.builder 创建了一个对话框,其中显示了可以检查的多选项目列表。 我设置了初始的项目名称集及其检查状态: builder.setMultiChoiceItems(
应用拦截短信并显示消息的对话框。 但是我无法在我的 Test 类中解决我的 Dialog 错误。我做错了什么? (我还包含了我的其他 2 个文件)。 Eclipse 中显示错误:AlertDialog
我的 flutter 应用程序中出现了一个 Flutter AlertDiaog。使用 Flutter Driver,我无法点击 Flutter AlertDialog 或 AlertDialog 上
我是 Jetpack compose 的初学者。现在在我的应用程序屏幕中,AlertDialog 用于向用户显示一些信息。 根据文档,当用户在对话框外或后退按钮上单击时,将调用 onDismissRe
我有一个 AlertDialog 的自定义子类,它应该显示范围内所有可用 Wifi 网络的列表。 我通过创建该对话框的实例并调用 show() 来显示此对话框,并且我没有使用 AlertDialog.
我有一个 AlertDialog 的子类,它应该显示范围内所有可用 Wifi 网络的列表。 我希望对话框本身负责启动 Wifi 扫描并接收结果。 出于这个原因,我不能使用 AlertDialog.Bu
标题几乎说明了一切。我需要关于如何做到这一点的建议。我可能只是将外部适配器添加为 AlertDialog 上的 View,以使其不那么复杂,但我仍然不知道如何与 交互来自内部适配器的 AlertDia
我想自定义 V7 AlertDialog 的 TITLE 颜色。 SetCustomTitle() 似乎无法与 android.support.v7.app.AlertDialog 一起使用。我可以看
创建 AlertDialog 然后显示和显示 AlertDialog.Builder 本身之间的主要区别是什么? 例如。我可以有一个像这样的 AlertDialog.Builder: AlertDi
关于我们什么时候应该使用 android.app.AlertDialog,或者我们什么时候应该使用 android.support.v7.app.AlertDialog,是否有任何指南? 因为,如果我
我是一名优秀的程序员,十分优秀!