Android复习对话框、菜单、加法器、进度条综合小练习

 

Android复习对话框、菜单、加法器、进度条综合小练习

 

                 ————安德风QQ1652102745

 

PS:本次练习简单介绍:本次练习通过选项菜单实现:

①普通对话框②自定义对话框③进度条对话框④时间对话框⑤日期对话框功能实现

 

 

一、最终效果演示

 

 

二、布局设计

1、主界面布局设计Activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <Button
10         android:id="@+id/btn"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="登录"
14         app:layout_constraintBottom_toBottomOf="parent"
15         app:layout_constraintEnd_toEndOf="parent"
16         app:layout_constraintStart_toStartOf="parent"
17         app:layout_constraintTop_toTopOf="parent" />
18 
19 </androidx.constraintlayout.widget.ConstraintLayout>

2、加法器布局设计activity_computer1.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7    >
 8 
 9     <LinearLayout
10         android:layout_width="437dp"
11         android:layout_height="72dp"
12         android:orientation="horizontal"
13         app:layout_constraintBottom_toBottomOf="parent"
14         app:layout_constraintEnd_toEndOf="parent"
15         app:layout_constraintStart_toStartOf="parent"
16         app:layout_constraintTop_toTopOf="parent">
17 
18         <EditText
19             android:id="@+id/j1"
20             android:layout_width="0dp"
21             android:layout_height="wrap_content"
22             android:layout_weight="1"
23             android:ems="10"
24             android:gravity="center"
25             android:hint="加法1"
26             android:inputType="textPersonName"
27             android:textSize="34sp" />
28 
29         <TextView
30             android:id="@+id/textView2"
31             android:layout_width="0dp"
32             android:layout_height="wrap_content"
33             android:layout_weight="1"
34             android:gravity="center"
35             android:text="+"
36             android:textSize="34sp" />
37 
38         <EditText
39             android:id="@+id/j2"
40             android:layout_width="0dp"
41             android:layout_height="wrap_content"
42             android:layout_weight="1"
43             android:ems="10"
44             android:gravity="center"
45             android:hint="加法2"
46             android:inputType="textPersonName"
47             android:textSize="34sp" />
48 
49     </LinearLayout>
50 </androidx.constraintlayout.widget.ConstraintLayout>

3、第二页面跳转界面布局activity_main2.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:background="@drawable/p2"
 8     tools:context=".Main2Activity">
 9 
10     <Button
11         android:id="@+id/bt"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:layout_marginTop="164dp"
15         android:onClick="btonClick"
16         android:text="返回"
17         app:layout_constraintEnd_toEndOf="parent"
18         app:layout_constraintHorizontal_bias="0.535"
19         app:layout_constraintStart_toStartOf="parent"
20         app:layout_constraintTop_toTopOf="parent" />
21 </androidx.constraintlayout.widget.ConstraintLayout>

 

4、用户登录对话框界面qq.xml

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     xmlns:tools="http://schemas.android.com/tools"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent">
 8 
 9     <LinearLayout
10         android:layout_width="444dp"
11         android:layout_height="110dp"
12         android:orientation="vertical"
13         app:layout_constraintBottom_toBottomOf="parent"
14         app:layout_constraintEnd_toEndOf="parent"
15         app:layout_constraintStart_toStartOf="parent"
16         app:layout_constraintTop_toTopOf="parent">
17 
18         <LinearLayout
19             android:layout_width="match_parent"
20             android:layout_height="60dp"
21             android:orientation="horizontal">
22 
23             <TextView
24                 android:id="@+id/textView2"
25                 android:layout_width="wrap_content"
26                 android:layout_height="wrap_content"
27                 android:layout_weight="1"
28                 android:gravity="center"
29                 android:text="用户名:"
30                 android:textSize="34sp" />
31 
32             <EditText
33                 android:id="@+id/user"
34                 android:layout_width="wrap_content"
35                 android:layout_height="wrap_content"
36                 android:layout_weight="1"
37                 android:ems="10"
38                 android:inputType="textPersonName"
39                 android:textSize="20sp" />
40         </LinearLayout>
41 
42         <LinearLayout
43             android:layout_width="match_parent"
44             android:layout_height="wrap_content"
45             android:orientation="horizontal">
46 
47             <TextView
48                 android:id="@+id/textView3"
49                 android:layout_width="wrap_content"
50                 android:layout_height="wrap_content"
51                 android:layout_weight="1"
52                 android:gravity="center"
53                 android:text="密 码  :"
54                 android:textSize="34sp" />
55 
56             <EditText
57                 android:id="@+id/pwd"
58                 android:layout_width="wrap_content"
59                 android:layout_height="wrap_content"
60                 android:layout_weight="1"
61                 android:ems="10"
62                 android:inputType="textPassword" />
63         </LinearLayout>
64     </LinearLayout>
65 </androidx.constraintlayout.widget.ConstraintLayout>

 

 

三、功能实现

1、MainActivity.java

 

  1 package com.example.computer;
  2 
  3 import androidx.annotation.NonNull;
  4 import androidx.appcompat.app.AlertDialog;
  5 import androidx.appcompat.app.AppCompatActivity;
  6 
  7 import android.app.DatePickerDialog;
  8 import android.app.ProgressDialog;
  9 import android.app.TimePickerDialog;
 10 import android.content.DialogInterface;
 11 import android.content.Intent;
 12 import android.os.Bundle;
 13 import android.view.Menu;
 14 import android.view.MenuInflater;
 15 import android.view.MenuItem;
 16 import android.view.View;
 17 import android.widget.Button;
 18 import android.widget.DatePicker;
 19 import android.widget.EditText;
 20 import android.widget.TextView;
 21 import android.widget.TimePicker;
 22 import android.widget.Toast;
 23 
 24 import java.util.Calendar;
 25 
 26 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 27 Button btn;//QQ登录界面
 28 int sum=0;//加法器求和
 29 int count = 0;//进度条进程初始值为0
 30 ProgressDialog d;//全局声明进度条对话框
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_main);
 35         btn=findViewById(R.id.btn);
 36         btn.setOnClickListener(this);
 37 
 38     }
 39 
 40     @Override
 41     public boolean onCreateOptionsMenu(Menu menu) {
 42        getMenuInflater().inflate(R.menu.optionsmenu,menu);
 43         return true;
 44     }
 45 
 46     @Override
 47     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
 48         switch (item.getItemId()){
 49             //选项菜单1(计算器)功能实现
 50            case  R.id.jfq:
 51                AlertDialog.Builder builder=new AlertDialog.Builder(this);
 52                builder.setTitle("加法器对话框:");
 53                builder.setIcon(R.drawable.ic_launcher_background);
 54                builder.setMessage("请填写加法器基本信息");
 55                final View jiafaqi = getLayoutInflater().inflate(R.layout.activity_computer1, null);
 56                builder.create();
 57                builder.setView(jiafaqi);
 58 
 59                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
 60                    @Override
 61                    public void onClick(DialogInterface dialog, int which) {
 62                        EditText jia1=jiafaqi.findViewById(R.id.j1);
 63                        EditText jia2=jiafaqi.findViewById(R.id.j2);
 64                        String j1=jia1.getText().toString();
 65                        String j2=jia2.getText().toString();
 66                        int sum1=Integer.valueOf(j1).intValue();
 67                        int sum2=Integer.valueOf(j2).intValue();
 68                        sum=sum1+sum2;
 69                        j1=String.valueOf(sum);
 70 
 71                        Toast.makeText(MainActivity.this, j1, Toast.LENGTH_SHORT).show();
 72 
 73                    }
 74                });
 75 
 76                builder.setNegativeButton("取消",null);
 77                builder.show();
 78            break;
 79 
 80 
 81 
 82 
 83             //切换布局
 84             case R.id.qiehuan:
 85                 Intent intent=new Intent(MainActivity.this,Main2Activity.class);
 86                 startActivity(intent);break;
 87 
 88 
 89 
 90 
 91 
 92 
 93             //实现进度条对话框
 94             case  R.id.progress:
 95                 //实例化进度条对话框
 96                 d=new ProgressDialog(MainActivity.this);
 97                 d.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条对话框样式:长方形
 98                 d.setTitle("进度条对话框"); //设置进度条对话框的标题
 99                 d.setMessage("程序正在等待中……");//设置进度条对话框的信息内容
100                 d.setMax(100);//设置进度条对话框的进程最大值为100
101                 d.setIcon(R.drawable.ic_launcher_background);
102 
103 
104                 d.setIndeterminate(false);// 设置进度条对话框是否可以按退回键取消
105                 d.setCancelable(true);//设置进度条对话框按返回键取消
106                 d.show();//显示进度条对话框
107 
108 
109     //进度条进程实现参考博客:https://www.cnblogs.com/94cool/p/3231797.html
110               new  Thread(){   //新建进程方法
111                   @Override
112                   public void run(){  //进程运行方法
113                       try {   //try抓取异常 将执行进度条进程直到加载满为止
114                           while(count<=100){
115                               //由线程来控制进度
116                               d.setProgress(count++);//进度条进度不断增加
117                               Thread.sleep(100);//直到进度条进程达到100终止
118 
119                           }
120 
121 //                          d.dismiss();//取消进度 还可以用d.cancel()方法表示取消
122 
123                       }
124 
125 
126                       catch (Exception e) {   //catch进程没有异常直接取消
127                          d.dismiss();      //取消进度 还可以用d.cancel()方法表示取消
128 
129                       }
130                       //进度条对话框运行完后跳转到第二个activity页面
131 //                      Toast.makeText(MainActivity.this, "恭喜您运行成功", Toast.LENGTH_SHORT).show();
132                       Intent intent1=new Intent(MainActivity.this,Main2Activity.class);
133                       startActivity(intent1);
134                   }
135 
136               } .start();
137                 break;
138 
139 
140 
141 
142 
143 
144 
145             //日期对话框
146             case  R.id.data:
147                 Calendar ca=Calendar.getInstance();//实例化日历
148                 int year= ca.get(Calendar.YEAR);//得到日历中的年
149                 int month= ca.get(Calendar.MONTH);//得到日历中的月
150                 final int day= ca.get(Calendar.DAY_OF_MONTH);//得到日历中的日
151 
152                 DatePickerDialog datePickerDialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
153                     @Override
154                     public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
155                         Toast.makeText(MainActivity.this, "当前日期:"+year+""+(month+1)+""+day+"", Toast.LENGTH_SHORT).show();
156                     }
157                 }, year, month, day);
158 
159                 datePickerDialog.show();//显示日期对话框
160 
161                 break;
162 
163             //时间对话框
164             case  R.id.time:
165                 Calendar ca2=Calendar.getInstance();//实例化日历
166                  final int hour=ca2.get(Calendar.HOUR_OF_DAY);//得到日历中的小时(参数设置为:HOUR_OF_DAY 输出就是24进行时)
167                  int minute=ca2.get(Calendar.MINUTE);//得到日历中的分钟
168 
169                 TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
170                     @Override
171                     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
172                         Toast.makeText(MainActivity.this, "当前时间:"+hour+":"+minute, Toast.LENGTH_SHORT).show();
173                     }
174                 }, hour, minute, true);
175                 timePickerDialog.show();//显示时间对话框
176 
177                 break;
178 
179                 //普通对话框
180             case R.id.pt:
181                 AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
182                 builder1.setTitle("普通对话框");
183                 builder1.setMessage("您确定要退出吗?");
184                 builder1.setIcon(R.drawable.ic_launcher_background);
185                 builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
186                     @Override
187                     public void onClick(DialogInterface dialog, int which) {
188                         Toast.makeText(MainActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
189                     }
190                 });
191                 builder1.setNegativeButton("取消",null);
192                 builder1.show();
193                 break;
194 
195 
196         }
197         return super.onOptionsItemSelected(item);
198     }
199 
200 
201 
202 //实例化QQ登录对话框
203     @Override
204     public void onClick(View v) {
205         AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
206         builder.setTitle("用户登录");//QQ对话框标题设置
207         builder.setIcon(R.drawable.ic_launcher_background);//QQ对话框图标设置
208         builder.setMessage("请输入用户名或者密码");//QQ对话框内容信息设置
209       final   View  QQ =getLayoutInflater().inflate(R.layout.qq,null);//孵化方法将QQ对话框布局设计导入
210        builder.setView(QQ);//加载布局
211        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
212            @Override
213            public void onClick(DialogInterface dialog, int which) {
214                EditText username=QQ.findViewById(R.id.user);//寻找孵化布局的QQ登录对话框中用户名ID
215                EditText Passworld=QQ.findViewById(R.id.pwd);//寻找孵化布局的QQ登录对话框中密码ID
216                String name=username.getText().toString();
217                String pwd=Passworld.getText().toString();
218              if (name.equals("adf520")||pwd.equals("123")){
219                  Toast.makeText(MainActivity.this, "恭喜您登录成功", Toast.LENGTH_SHORT).show();
220              }
221              else{
222                  Toast.makeText(MainActivity.this, "抱歉,登录失败,请检查一下您的用户名或者密码", Toast.LENGTH_SHORT).show();
223              }
224            }
225        });
226 
227        builder.setNegativeButton("取消",null);
228        builder.show();//显示QQ登录对话框
229 
230 
231     }
232 
233 
234 
235 }

 

2、Main2Activity.java

 1 package com.example.computer;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 
10 public class Main2Activity extends AppCompatActivity {
11 Button bt;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main2);
16         bt=findViewById(R.id.bt);
17 
18     }
19 
20     public void btonClick(View view) {
21         Intent intent=new Intent(Main2Activity.this,MainActivity.class);
22         startActivity(intent);
23     }
24 }

四、菜单布局文件

1、menu/optionmenu.xml选项菜单布局

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 3 
 4     <item
 5         android:id="@+id/jfq"
 6         android:title="加法器" />
 7     <item
 8         android:id="@+id/qiehuan"
 9         android:title="切换布局" />
10     <item
11         android:id="@+id/progress"
12         android:title="进度条对话框" />
13     <item
14         android:id="@+id/time"
15         android:title="时间对话框" />
16     <item
17         android:id="@+id/data"
18         android:title="日期对话框" />
19     <item
20         android:id="@+id/pt"
21         android:title="普通对话框" />
22 </menu>

 

posted @ 2020-05-11 13:16  安德风  阅读(354)  评论(0编辑  收藏  举报