gpt4 book ai didi

android - ProgressDialog.show() 的上下文参数是什么?

转载 作者:行者123 更新时间:2023-12-05 06:46:58 27 4
gpt4 key购买 nike

我是安卓新手。我正在尝试构建一个简单的 android 应用程序:用户单击按钮,进度对话框出现 5 秒钟。我使用了 ProgressDialog.show() 并遇到了上下文参数问题。这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnDialog2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnDialog2" />
</LinearLayout>

这是我的代码:

public class Dialog22Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btnDialog2 = (Button)findViewById(R.id.btnDialog2);
btnDialog2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
final ProgressDialog dialog = ProgressDialog.show(getBaseContext(),
"Progress dialog", "Loading...", true);
new Thread(new Runnable() {

@Override
public void run() {
try {
Thread.sleep(5000);
dialog.dismiss();
} catch (InterruptedException e) {

}

}
}).start();
}
});
}
}

如果我将 ProgressDialog.show() 的上下文参数从 getBaseContext() 更改为 v.getContext(),我的程序将正常运行。所以我想问一下这里的context参数是什么意思?感谢您的帮助。

最佳答案

只要使用 Dialog22Activity.this 而不是你想要的 getContext() 或 WhatEverContextFunction() ,只要你在这个类中,你就会很酷:)

关于android - ProgressDialog.show() 的上下文参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12954590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com