第五章 界面之间的传递(例) 2.7

1. LoginActivity

 1 package com.example.infotransmission;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.text.TextUtils;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 import android.widget.Toast;
11 
12 import androidx.annotation.Nullable;
13 
14 public class LoginActivity extends Activity {
15 
16     private Button mButtonRegister;
17     private EditText mEditUsername;
18     private EditText mEditPassword;
19 
20 
21     @Override
22     protected void onCreate(@Nullable Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_login);
25         // 初始话部件
26         initView();
27 
28         // 给按钮添加监听事件
29         initListener();
30     }
31 
32     private void initListener() {
33         mButtonRegister.setOnClickListener(new View.OnClickListener() {
34             @Override
35             public void onClick(View v) {
36                 // 处理注册
37                 handlerRegister();
38             }
39         });
40     }
41 
42     private void handlerRegister() {
43         String usernameText = mEditUsername.getText().toString().trim();
44         String passwordText = mEditPassword.getText().toString().trim();
45 
46         // 这个方法里面有两个判断,一个是判空,一个是长度
47         if (TextUtils.isEmpty(usernameText)) {
48             Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
49             // return的作用是终止当前函数的操作,切记不要忘了
50             return;
51         }
52 
53         if (TextUtils.isEmpty(passwordText)) {
54             Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
55             return;
56         }
57 
58         // 到此即可以注册
59         Intent intent = new Intent(this, RegisterResult.class);
60         intent.putExtra("username", usernameText);
61         intent.putExtra("password", passwordText);
62         startActivity(intent);
63         // 结束后关闭该Activity
64         this.finish();
65     }
66 
67     private void initView() {
68         mEditUsername = (EditText) this.findViewById(R.id.edit_username);
69         mEditPassword = (EditText) this.findViewById(R.id.edit_password);
70         mButtonRegister = this.findViewById(R.id.button_register);
71     }
72 }

 

2. RegisterResult:

 1 package com.example.infotransmission;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.widget.TextView;
 7 
 8 import androidx.annotation.Nullable;
 9 
10 public class RegisterResult extends Activity {
11 
12     private TextView mRegisterResult;
13 
14     @Override
15     protected void onCreate(@Nullable Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_loginresult);
18         mRegisterResult = (TextView) this.findViewById(R.id.registerResult);
19 
20         Intent intent = getIntent();
21         if (intent != null) {
22             String username = intent.getStringExtra("username");
23             // 密码就不用显示了,直接传入服务器
24             mRegisterResult.setText("恭喜[" + username + "]! 已经注册成功");
25         }
26     }
27 }

 

3. activity_login:

 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:orientation="vertical">
 6 
 7     <EditText
 8         android:id="@+id/edit_username"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:hint="请输入账号"/>
12 
13     <EditText
14         android:id="@+id/edit_password"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:inputType="textPassword"
18         android:hint="请输入密码"/>
19 
20     <Button
21         android:id="@+id/button_register"
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content"
24         android:text="注册"/>
25 
26 </LinearLayout>

 

4. activity_loginresult

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7 
 8     <TextView
 9         android:id="@+id/registerResult"
10         android:layout_width="match_parent"
11         android:layout_height="match_parent"
12         android:textSize="20sp"
13         android:gravity="center"/>
14 
15 </LinearLayout>

 

5. 运行结果

第五章 界面之间的传递(例) 2.7

 

上一篇:Android SharedPreferences存储电话号码


下一篇:android listview添加headview,满满干货指导