Java教程

团队冲刺博客(三)

本文主要是介绍团队冲刺博客(三),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

这次是添加了头像及用户名的界面,点击头像,进入相对应的用户界面,

以及增加了一个点击安卓APP一键退出的功能。

效果如下:

部分代码如下:

点击头像进入用户登录界面:

HeadPortraitActivity.java

 1 import androidx.appcompat.app.AppCompatActivity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.widget.Button;
 6 
 7 import com.michael.cloudphotos.MainActivity;
 8 import com.michael.cloudphotos.R;
 9 
10 public class HeadPortraitActivity extends AppCompatActivity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_head_portrait);
16         Button mlogin = findViewById(R.id.login);
17         Button msignin = findViewById(R.id.sign_in);
18         mlogin.setOnClickListener(new View.OnClickListener() {
19             @Override
20             public void onClick(View v) {
21                 Intent intent=null;
22                 //返回主页
23                 intent=new Intent(HeadPortraitActivity.this,MainActivity.class);
24                 startActivity(intent);
25             }
26         });
27         msignin.setOnClickListener(new View.OnClickListener() {
28             @Override
29             public void onClick(View v) {
30                 Intent intent=null;
31                 //跳转到注册界面
32                 intent=new Intent(HeadPortraitActivity.this,register.class);
33                 startActivity(intent);
34             }
35         });
36     }
37 }

一键退出的后台代码:

Exit.java

 1 import android.app.Activity;
 2 import android.app.Application;
 3 
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 public class Exit extends Application {
 8     private List<Activity> activityList = new ArrayList<>();
 9     private static Exit instance;
10 
11     public Exit(){}
12     public static Exit getInstance(){
13         if(null == instance){
14             instance = new Exit();
15         }
16         return instance;
17     }
18 
19     //添加Activity到容器中
20     public void addActivity(Activity activity){
21         activityList.add(activity);
22     }
23 
24     //遍历所有Activity并finish
25     public void exit(){
26         for(Activity activity : activityList){
27             //依次关闭
28             activity.finish();
29             //Log.i("activity", "del_size:" + activityList.size());
30             //Log.i("activity", "del_name:" + activity.getIntent());
31         }
32         //强制退出
33         System.exit(0);
34     }
35 }

在MainActivity.java中,头像及一键退出的代码为:

 1 NavigationView navigationView = findViewById(R.id.nav_view);//获取navigationView
 2 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
 3             @Override
 4             public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
 5                 switch (menuItem.getItemId()){
 6                     case R.id.nav_exit:
 7                         Exit.getInstance().exit();
 8                         break;
 9                 }
10                 return false;
11             }
12         });
13 
14         //头像——跳转登录界面
15         View headView=navigationView.getHeaderView(0);
16         mHead_portrait= headView.findViewById(R.id.Head_portrait);//通过headView来获取头布局中的组件
17         mHead_portrait.setOnClickListener(new View.OnClickListener() {
18             @Override
19             public void onClick(View v) {
20                 Intent intent = new Intent(MainActivity.this, HeadPortraitActivity.class);
21                 startActivity(intent);
22             }
23         });

activity_head_portrait.xml(此为点击头像后进入的登录界面)

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout 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:orientation="vertical"
  8     tools:context=".LoginAndRigister.HeadPortraitActivity">
  9 
 10     <LinearLayout
 11         android:layout_width="match_parent"
 12         android:layout_height="wrap_content"
 13         android:layout_above="@+id/id_relativeLayout"
 14         android:layout_marginBottom="30dp"
 15         android:gravity="center"
 16         android:orientation="horizontal">
 17 
 18         <ImageView
 19             android:layout_width="wrap_content"
 20             android:layout_height="wrap_content"
 21             android:src="@mipmap/ic_cream"
 22             android:layout_marginTop="30dp"
 23             android:layout_marginBottom="40dp"/>
 24 
 25     </LinearLayout>
 26 
 27     <LinearLayout
 28         android:id="@+id/id_relativeLayout"
 29         android:layout_width="match_parent"
 30         android:layout_height="wrap_content"
 31         android:layout_centerInParent="true"
 32         android:orientation="vertical">
 33 
 34         <LinearLayout
 35             android:id="@+id/id_LinearLayout"
 36             android:layout_width="match_parent"
 37             android:layout_height="match_parent"
 38             android:layout_marginLeft="10dp"
 39             android:layout_marginRight="10dp"
 40             android:layout_weight="1"
 41             android:gravity="center_vertical"
 42             android:orientation="horizontal">
 43 
 44             <TextView
 45                 android:id="@+id/id_text"
 46                 android:layout_width="wrap_content"
 47                 android:layout_height="wrap_content"
 48                 android:layout_gravity="center_vertical"
 49                 android:text="账 号"
 50                 android:textSize="20dp"
 51                 android:layout_marginLeft="15dp"/>
 52 
 53             <EditText
 54                 android:id="@+id/id"
 55                 android:layout_width="match_parent"
 56                 android:layout_height="wrap_content"
 57                 android:layout_gravity="center_vertical"
 58                 android:layout_marginLeft="20dp"
 59                 android:layout_marginRight="20dp"
 60                 android:layout_toRightOf="@+id/id_text"
 61                 android:layout_weight="1"
 62                 android:background="@null"
 63                 android:ems="10"
 64                 android:hint="请输入账号"
 65                 android:singleLine="true" />
 66 
 67         </LinearLayout>
 68 
 69     </LinearLayout>
 70 
 71     <LinearLayout
 72         android:id="@+id/password_relativeLayout"
 73         android:layout_width="match_parent"
 74         android:layout_height="wrap_content"
 75         android:layout_centerInParent="true"
 76         android:layout_marginTop="10dp"
 77         android:layout_below="@+id/id_relativeLayout"
 78         android:orientation="vertical">
 79 
 80         <LinearLayout
 81             android:id="@+id/password_LinearLayout"
 82             android:layout_width="match_parent"
 83             android:layout_height="match_parent"
 84             android:layout_marginLeft="10dp"
 85             android:layout_marginRight="10dp"
 86             android:layout_weight="1"
 87             android:gravity="center_vertical"
 88             android:orientation="horizontal">
 89 
 90             <TextView
 91                 android:id="@+id/password_text"
 92                 android:layout_width="wrap_content"
 93                 android:layout_height="wrap_content"
 94                 android:layout_gravity="center_vertical"
 95                 android:text="密 码"
 96                 android:textSize="20dp"
 97                 android:layout_marginLeft="15dp"/>
 98 
 99             <EditText
100                 android:id="@+id/password"
101                 android:layout_width="match_parent"
102                 android:layout_height="wrap_content"
103                 android:layout_gravity="center_vertical"
104                 android:layout_marginLeft="20dp"
105                 android:layout_marginRight="20dp"
106                 android:layout_toRightOf="@+id/password_text"
107                 android:layout_weight="1"
108                 android:background="@null"
109                 android:ems="10"
110                 android:hint="请输入密码"
111                 android:singleLine="true" />
112 
113         </LinearLayout>
114     </LinearLayout>
115 
116     <LinearLayout
117         android:layout_width="match_parent"
118         android:layout_height="wrap_content"
119         android:layout_below="@+id/password_relativeLayout"
120         android:gravity="center"
121         android:orientation="horizontal">
122 
123         <Button
124             android:id="@+id/login"
125             android:layout_width="150dp"
126             android:layout_height="wrap_content"
127             android:layout_marginTop="5dp"
128             android:layout_marginLeft="30dp"
129             android:text="登 录"
130             android:background="@color/colorPrimary"
131             android:textColor="#ffffff"
132             android:textSize="18dp"/>
133 
134         <Button
135             android:id="@+id/sign_in"
136             android:layout_width="150dp"
137             android:layout_height="wrap_content"
138             android:layout_below="@+id/password_relativeLayout"
139             android:layout_marginRight="20dp"
140             android:layout_marginLeft="20dp"
141             android:layout_toRightOf="@+id/login"
142             android:layout_marginTop="5dp"
143             android:background="@color/colorPrimary"
144             android:text="注 册"
145             android:textColor="#ffffff"
146             android:textSize="18dp"/>
147 
148     </LinearLayout>
149 </RelativeLayout>

activity_register.xml(此为注册界面)

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout 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:orientation="vertical"
 8     tools:context=".LoginAndRigister.register">
 9 
10     <RelativeLayout
11         android:id="@+id/register_id"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content">
14 
15         <TextView
16             android:id="@+id/register_id_text"
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:text="账号"/>
20 
21         <EditText
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:layout_toRightOf="@+id/register_id_text"
25             android:hint="请输入账号"/>
26 
27     </RelativeLayout>
28 
29     <RelativeLayout
30         android:id="@+id/register_password"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content">
33 
34         <TextView
35             android:id="@+id/register_password_text"
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content"
38             android:text="密码"/>
39 
40         <EditText
41             android:layout_width="wrap_content"
42             android:layout_height="wrap_content"
43             android:layout_toRightOf="@+id/register_password_text"
44             android:hint="请输入密码"/>
45 
46     </RelativeLayout>
47 
48     <RelativeLayout
49         android:id="@+id/register_phone"
50         android:layout_width="wrap_content"
51         android:layout_height="wrap_content">
52 
53         <TextView
54             android:id="@+id/register_phone_text"
55             android:layout_width="wrap_content"
56             android:layout_height="wrap_content"
57             android:text="电话号码"/>
58 
59         <EditText
60             android:layout_width="wrap_content"
61             android:layout_height="wrap_content"
62             android:layout_toRightOf="@+id/register_phone_text"
63             android:hint="请输入账号"/>
64 
65     </RelativeLayout>
66 
67     <RelativeLayout
68         android:layout_width="wrap_content"
69         android:layout_height="wrap_content">
70 
71         <Button
72             android:layout_width="wrap_content"
73             android:layout_height="wrap_content"
74             android:text="注册"/>
75 
76     </RelativeLayout>
77 </LinearLayout>

界面还是略有一些粗糙,后续还会继续优化界面,使其变得更加美观。

这篇关于团队冲刺博客(三)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!