【问题标题】:progress dialog is not showing the spinner in android version 8.0 but it still works进度对话框未显示 android 8.0 版中的微调器,但它仍然有效
【发布时间】:2019-03-08 02:45:51
【问题描述】:

在我的应用程序中,单击按钮时应该会显示进度对话框。现在显示了进度对话框,但没有微调器或加载栏(抱歉,我不确定它叫什么)

这是我的进度对话框在 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);
                  }
           }
     }

谁能帮我解决这个问题?

【问题讨论】:

    标签: android progressdialog


    【解决方案1】:

    进度对话框

    已从 SDK 26 中弃用。您应该使用

    进度条

    用法:

    在您的布局中添加视图:

    <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"/>
    

    在您的活动中声明您的progressBar

    ProgressBar progressBar;
    

    onCreate 获取视图

    progressBar = findViewById(R.id.progressBar);
    

    当你需要显示progressBar时:

    progressBar.setVisibility(View.VISIBLE);
    

    当你需要它离开时:

    progressBar.setVisibility(View.GONE);
    

    更多详情请查看ProgressBar docs

    ProgressBar 启动时禁用用户交互可能会有所帮助。

    编辑

    禁用交互

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    

    重新启用交互:

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    

    编辑 2

    您无需按照 Material Design 模式 (Progress Indicators) 复制 ProgressDialog 视觉效果。

    编辑 3

    这是我在 cmets 中对您所说的示例。它是 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"
    

    正如您在 cmets 中所问的,这只是一个示例,不是推荐。这不是您在生活中看到的最美丽的布局,但它会给您一个北方。

    【讨论】:

    • 你知道如何设置title和setOutsideclickable false吗?
    • @overflowstack ProgressBar 本身没有 setTitle 或 setText。要禁用点击,请查看我的编辑。
    • 那么我应该用什么来显示进度对话框之类的东西?
    • @overflowstack 您应该创建一个View,然后为标题和文本添加Textviews。在此视图中添加进度。
    • @overflowstack 看看我的编辑。如果它回答了您的问题,请将其标记为已解决。
    猜你喜欢
    • 2011-10-17
    • 2012-06-11
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多