我如何在Progressdialog旋转中制作微调框?
我已经实现了progressDialog
,但是当我的活动开始并且它工作或从互联网获取信息时,它不会旋转。我已经在Stackoverflow和其他地方尝试了很多解决方案,但它仍然不旋转 - progressDialog
弹出ok,但轮子不旋转。许多建议都一致使用AsyncTask
,开始一个新的线程/ runnable而不是UI线程,但仍然没有运气。我会很感激任何解决方案。我如何在Progressdialog旋转中制作微调框?
与#2职位的帮助,我想这在我的AsyncTask
的OnPreExecute
......但现在progressDialog
甚至不露面!:
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewContact.this);
new Thread(new Runnable() {
@Override
public void run() {
// Showing progress dialog
pDialog.setMessage("Loading...");
pDialog.show();
}
});
}
的代码我一直在使用,这表明在progressDialog
但它不旋转,方法是:
protected void onPreExecute() {
pDialog = new ProgressDialog(NewContact.this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
super.onPreExecute();
}
下面是该活动的完整代码结构:
public class NewContact extends AppCompatActivity implements android.widget.CompoundButton.OnCheckedChangeListener {
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_contact);
StringRequest stringRequest = new StringRequest(Request.Method.POST, NewContact_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
}
// Load data in background
class LoadContact extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewContact.this);
new Thread(new Runnable() {
@Override
public void run() {
// Showing progress dialog
pDialog.setMessage("Loading...");
pDialog.show();
}
});
}
@Override
protected Void doInBackground(Void... voids) {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
hidePDialog();
}
}
@Override
protected void onResume() {
super.onResume();
LoadContact loadContact = new LoadContact();
loadContact.execute();
}
public void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
在我res/values/styles.xml
我:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
我不记得曾经进入了styles.xml
和设置这些值。我的应用程序工作正常(除了微调),但=Theme...
部分styles.xml
的都是红色的,当我把鼠标放在他们,我得到的消息:
Cannot resolve symbol 'Theme.AppCompat'
Cannot resolve symbol 'Theme.AppCompat.Light'
Cannot resolve symbol 'Theme.AppCompat.Light.DarkActionBar'
难道这是与我的问题呢?
你不需要在新线程中包装onPreExecute
中的东西,你没有做任何阻止UI的东西。在返回null之前,您似乎还有一个额外的右括号。我改变它看起来像这样:
// Load data in background
class LoadContact extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
// Showing progress dialog
pDialog.setMessage("Loading...");
pDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
hidePDialog();
}
}
做出这些更改后,它对我来说工作正常。需要检查的一点是,您没有在设备上的“开发人员选项”中关闭动画。如果他们被关闭(就像他们需要进行浓缩咖啡测试),那么微调不会显示。
我认为我的问题与主题有关,仍然在处理它。虽然对开发者选项感兴趣。 – CHarris
您之前显示进度对话框的方法是正确的。我认为进度对话框没有旋转,因为主UI线程上的StringRequest完成了一些繁重的工作,而且我无法理解这个StringRequest做了什么? – Passiondroid
分享你的主题为该活动 –
Downvoter:请评论原因。想到这个问题,并尝试解决,不知道如何,这就是为什么我张贴。此刻离开机器,马上发布主题,谢谢。所有字符串请求都会从服务器获取一些小文本。 – CHarris