4.progress&progressdialog
java代码
1 package com.example.myapplication; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.app.Dialog; 6 import android.app.ProgressDialog; 7 import android.content.DialogInterface; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.Toast; 12 13 public class proActivity extends AppCompatActivity { 14 private Button pro1,pro2; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_pro); 20 pro1=findViewById(R.id.pro1); 21 pro2=findViewById(R.id.pro2); 22 pro1.setOnClickListener(new View.OnClickListener() { 23 @Override 24 public void onClick(View view) { 25 ProgressDialog progressDialog=new ProgressDialog(proActivity.this); 26 progressDialog.setTitle("登陆中~~~"); 27 progressDialog.setMessage("正在登陆"); 28 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消登陆", new DialogInterface.OnClickListener() { 29 @Override 30 public void onClick(DialogInterface dialogInterface, int i) { 31 Toast.makeText(proActivity.this,"取消登陆",Toast.LENGTH_SHORT).show(); 32 } 33 }); 34 progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 35 @Override 36 public void onCancel(DialogInterface dialogInterface) { 37 Toast.makeText(proActivity.this,"加载完成",Toast.LENGTH_SHORT).show(); 38 } 39 }); 40 progressDialog.show(); 41 } 42 }); 43 pro2.setOnClickListener(new View.OnClickListener() { 44 @Override 45 public void onClick(View view) { 46 ProgressDialog progressDialog=new ProgressDialog(proActivity.this); 47 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 48 progressDialog.setTitle("提示"); 49 progressDialog.setMessage("正在下载..."); 50 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "继续下载", new DialogInterface.OnClickListener() { 51 @Override 52 public void onClick(DialogInterface dialogInterface, int i) { 53 54 } 55 }); 56 progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消下载", new DialogInterface.OnClickListener() { 57 @Override 58 public void onClick(DialogInterface dialogInterface, int i) { 59 60 } 61 }); 62 progressDialog.show(); 63 } 64 }); 65 } 66 }
xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:padding="15dp" 6 android:orientation="vertical" 7 > 8 <Button 9 android:layout_marginTop="200dp" 10 android:id="@+id/pro1" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:text="progressBar练习" 14 android:textAllCaps="false" 15 /> 16 17 <Button 18 android:layout_marginTop="200dp" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:text="progressDialog练习" 22 android:textAllCaps="false" 23 android:id="@+id/pro2"/> 24 25 </LinearLayout>