ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android:进度条加载

2021-12-03 18:34:58  阅读:161  来源: 互联网

标签:xh pDialog 进度条 import Android ProgressDialog android 加载


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:orientation="vertical">
<ImageView
    android:id="@+id/iv"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:background="@drawable/head"/>
<LinearLayout
    android:id="@+id/ll_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/iv"
    android:layout_centerVertical="true"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="15dp"
    android:background="#ffffff">
    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="账号:"
        android:textColor="#000"
        android:textSize="20sp"/>
    <EditText
        android:id="@+id/et_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:background="@null"
        android:padding="10dp"/>
</LinearLayout>
<LinearLayout
    android:id="@+id/ll_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/ll_number"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#ffffff">
    <TextView
        android:id="@+id/tv_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="密码:"
        android:textColor="#000"
        android:textSize="20sp"/>
    <EditText
        android:id="@+id/et_number2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:background="@null"
        android:padding="10dp"/>
</LinearLayout>
<Button
    android:id="@+id/btn_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/ll_password"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="50dp"
    android:background="#3C8DC4"
    android:text="登录"
    android:textColor="#ffffff"
    android:textSize="20sp"/>

</RelativeLayout>

 MainActivity.java:

package com.example.lenovo.myapplicationloading;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button xhButton01, xhButton02;

    int xh_count = 0;
    // 声明进度条对话框
    ProgressDialog xh_pDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 得到按钮对象
        xhButton02 = (Button) findViewById(R.id.btn_login);

        // 设置xhButton02的事件监听
        xhButton02.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {

                xh_count = 0;

                // 创建ProgressDialog对象
                xh_pDialog = new ProgressDialog(MainActivity.this);

                // 设置进度条风格,风格为圆形,旋转的
                xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                // 设置ProgressDialog 标题
                xh_pDialog.setTitle("提示");

                // 设置ProgressDialog提示信息
                xh_pDialog.setMessage("正在加载中……");

                // 设置ProgressDialog标题图标
                xh_pDialog.setIcon(R.drawable.shape_progressbar_bg);

                // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
                xh_pDialog.setIndeterminate(false);

                // 设置ProgressDialog 进度条进度
                xh_pDialog.setProgress(100);

                // 设置ProgressDialog 是否可以按退回键取消
                xh_pDialog.setCancelable(true);

                // 让ProgressDialog显示
                xh_pDialog.show();

                new Thread() {
                    @Override
                    public void run() {
                        try {
                            while (xh_count <= 100) {
                                // 由线程来控制进度
                                xh_pDialog.setProgress(xh_count++);
                                Thread.sleep(100);
                            }
                            xh_pDialog.cancel();
                        } catch (Exception e) {
                            xh_pDialog.cancel();
                        }
                    }
                }.start();

            }

        });
    }

    // xhButton01的监听器类
    class Bt1DialogListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // 点击“确定”按钮取消对话框
            dialog.cancel();
        }
    }
}

标签:xh,pDialog,进度条,import,Android,ProgressDialog,android,加载
来源: https://blog.csdn.net/qq_45959399/article/details/121704605

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有