ICode9

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

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

2021-09-23 11:02:36  阅读:156  来源: 互联网

标签:Toast 界面 void private 第五章 intent import android 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. 运行结果

 

标签:Toast,界面,void,private,第五章,intent,import,android,2.7
来源: https://www.cnblogs.com/EndlessShw/p/15323202.html

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

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

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

ICode9版权所有