android 版本 8.0 中的进度对话框未显示微调器,但它仍然有效
progress dialog is not showing the spinner in android version 8.0 but it still works
在我的应用程序中,单击按钮时应显示进度对话框。现在显示了进度对话框,但没有微调器或加载栏(抱歉,我不确定它叫什么)
这是我的进度对话框在 android 8.0 真实设备
中的样子
这是我的进度对话框在 android 5.0
中的样子
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
progressDialog = new ProgressDialog(Login.this);
}
public void loginUser(View v) {
final String Email = emailText.getText().toString();
final String password = passwordText.getText().toString();
progressDialog.setMessage("Logging in....");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
if (TextUtils.isEmpty(Email)) {
emailText.setError("Email is required");
}
if (TextUtils.isEmpty(password)) {
passwordText.setError("password is required");
}
if ((!TextUtils.isEmpty(Email)) && (!TextUtils.isEmpty(password))) {
mAuth.signInWithEmailAndPassword(Email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toasty.error(getApplicationContext(), "Login Failed",
Toast.LENGTH_SHORT, true).show();
progressDialog.dismiss();
} else {
Toasty.success(getApplicationContext(), "Login Success",
Toast.LENGTH_SHORT, true).show();
Intent intent = new Intent(Login.this, Admins.class);
intent.putExtra("Email", emailText.getText().toString());
startActivity(intent);
}
}
}
谁能帮我解决这个问题?
ProgressDialog
已从 SDK 26 中弃用。您应该使用
ProgressBar
用法:
在您的布局中添加视图:
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@+id/parent"
app:layout_constraintBottom_toBottomOf="@+id/parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:indeterminate="true"
android:max="100"
android:visibility="gone"/>
在您的 activity
中声明您的 progressBar
ProgressBar progressBar;
在onCreate
获取视图
progressBar = findViewById(R.id.progressBar);
何时需要显示进度条:
progressBar.setVisibility(View.VISIBLE);
当你需要它消失时:
progressBar.setVisibility(View.GONE);
有关详细信息,请查看 ProgressBar 文档
在 ProgressBar
启动时禁用用户交互可能会有所帮助。
编辑
禁用交互
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
重新启用互动:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
编辑 2
您不需要根据 Material 设计模式 (Progress Indicators) 复制 ProgressDialog
视觉效果。
编辑 3
这是我在评论中对你说的话的一个例子。它是 View
内 ProgressBar 的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="16dp"
android:visibility="visible"
android:background="#EFEFEF">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Things to do"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:indeterminate="true"
android:max="100"
android:visibility="visible"
android:layout_below="@+id/title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please wait we are doing things..."
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/progressBar"
android:layout_toEndOf="@+id/progressBar"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
它会产生这样的东西:
您可以为 API 设置海拔 21 或更高:
android:elevation="10dp"
正如您在评论中所问,这只是一个例子,不是建议。这不是你在生活中看到的最美丽的布局,但它会给你一个北方。
在我的应用程序中,单击按钮时应显示进度对话框。现在显示了进度对话框,但没有微调器或加载栏(抱歉,我不确定它叫什么)
这是我的进度对话框在 android 8.0 真实设备
中的样子这是我的进度对话框在 android 5.0
中的样子 private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
progressDialog = new ProgressDialog(Login.this);
}
public void loginUser(View v) {
final String Email = emailText.getText().toString();
final String password = passwordText.getText().toString();
progressDialog.setMessage("Logging in....");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
if (TextUtils.isEmpty(Email)) {
emailText.setError("Email is required");
}
if (TextUtils.isEmpty(password)) {
passwordText.setError("password is required");
}
if ((!TextUtils.isEmpty(Email)) && (!TextUtils.isEmpty(password))) {
mAuth.signInWithEmailAndPassword(Email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toasty.error(getApplicationContext(), "Login Failed",
Toast.LENGTH_SHORT, true).show();
progressDialog.dismiss();
} else {
Toasty.success(getApplicationContext(), "Login Success",
Toast.LENGTH_SHORT, true).show();
Intent intent = new Intent(Login.this, Admins.class);
intent.putExtra("Email", emailText.getText().toString());
startActivity(intent);
}
}
}
谁能帮我解决这个问题?
ProgressDialog
已从 SDK 26 中弃用。您应该使用
ProgressBar
用法:
在您的布局中添加视图:
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@+id/parent"
app:layout_constraintBottom_toBottomOf="@+id/parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:indeterminate="true"
android:max="100"
android:visibility="gone"/>
在您的 activity
中声明您的progressBar
ProgressBar progressBar;
在onCreate
获取视图
progressBar = findViewById(R.id.progressBar);
何时需要显示进度条:
progressBar.setVisibility(View.VISIBLE);
当你需要它消失时:
progressBar.setVisibility(View.GONE);
有关详细信息,请查看 ProgressBar 文档
在 ProgressBar
启动时禁用用户交互可能会有所帮助。
编辑
禁用交互
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
重新启用互动:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
编辑 2
您不需要根据 Material 设计模式 (Progress Indicators) 复制 ProgressDialog
视觉效果。
编辑 3
这是我在评论中对你说的话的一个例子。它是 View
内 ProgressBar 的布局<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="16dp"
android:visibility="visible"
android:background="#EFEFEF">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Things to do"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:indeterminate="true"
android:max="100"
android:visibility="visible"
android:layout_below="@+id/title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please wait we are doing things..."
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/progressBar"
android:layout_toEndOf="@+id/progressBar"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
它会产生这样的东西:
您可以为 API 设置海拔 21 或更高:
android:elevation="10dp"
正如您在评论中所问,这只是一个例子,不是建议。这不是你在生活中看到的最美丽的布局,但它会给你一个北方。