ICode9

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

Android-登录测试

2022-07-11 13:33:53  阅读:331  来源: 互联网

标签:username Toast 登录 intent 测试 import Android password android


 

 第一步:做一个简单的UI登录界面

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout android:layout_height="match_parent"
 3     android:layout_width="match_parent"
 4     android:paddingTop="70dp"
 5     xmlns:android="http://schemas.android.com/apk/res/android">
 6 
 7    <ImageView
 8        android:id="@+id/im_one"
 9        android:layout_centerHorizontal="true"
10        android:src="@drawable/ic_launcher_background"
11        android:layout_width="wrap_content"
12        android:layout_height="wrap_content"/>
13 
14    <EditText
15        android:id="@+id/username"
16        android:hint="用户名"
17        android:layout_below="@+id/im_one"
18        android:layout_centerHorizontal="true"
19        android:paddingTop="20dp"
20        android:layout_height="wrap_content"
21        android:layout_width="200dp"/>
22 
23    <EditText
24        android:id="@+id/password"
25        android:layout_width="200dp"
26        android:layout_height="wrap_content"
27        android:layout_alignStart="@+id/username"
28        android:layout_below="@+id/username"
29        android:hint="密码"
30        android:inputType="textPassword" />
31 
32    <Button
33        android:id="@+id/login"
34        android:layout_width="wrap_content"
35        android:layout_height="wrap_content"
36        android:layout_below="@+id/password"
37        android:layout_centerHorizontal="true"
38        android:text="登录" />
39 
40 </RelativeLayout>
activity_main.xml

第二步:在Java代码中获取输入的用户名和密码,通过登录按钮的点击事件,去把输入的用户名和密码进行比对,然后通过意图和广播进行反馈登录结果。

MainActivity.java

 1 package com.example.mylogin;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.app.PendingIntent;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.util.Log;
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.EditText;
12 
13 public class MainActivity extends AppCompatActivity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19 
20         EditText et_username = findViewById(R.id.username);
21         EditText et_password = findViewById(R.id.password);
22 
23         Button login = findViewById(R.id.login);
24 
25         login.setOnClickListener(new View.OnClickListener() {
26             @Override
27             public void onClick(View v) {
28 
29                 String username =et_username.getText().toString();
30                 String password =et_password.getText().toString();
31                 Log.e("TAG", "username " + username);
32                 Log.e("TAG", "password " + password);
33                 Intent intent = new Intent();
34                 if (username.equals("123") && password.equals("abc")){
35                     intent.setAction("com.example.CUSTOM_INTENT");
36                     sendBroadcast(intent);
37                 } else{
38                     intent.setAction("com.example.error");
39                     sendBroadcast(intent);                }
40 
41             }
42         });
43     }
44 }
MainActivity

 AndroidManifest.xml

 

1  <receiver android:name="TestActivity"
2             android:exported="true">
3             <intent-filter>
4                 <action android:name="com.example.CUSTOM_INTENT" />
5                 <action android:name="com.example.error" />
6             </intent-filter>
7 
8         </receiver>
AndroidManifest.xml

 TestActivity.java

 

 1 package com.example.mylogin;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.widget.Toast;
 7 
 8 
 9 public class TestActivity extends BroadcastReceiver {
10     private final String ACTION_BOOT = "com.example.CUSTOM_INTENT";
11     @Override
12     public void onReceive(Context context, Intent intent) {
13         if (ACTION_BOOT.equals(intent.getAction())){
14             Toast.makeText(context, "登陆成功", Toast.LENGTH_SHORT).show();
15         } else {
16             Toast.makeText(context, "登录失败", Toast.LENGTH_SHORT).show();
17         }
18     }
19 }
TestActivity

 最后一步:进行登录测试

 

标签:username,Toast,登录,intent,测试,import,Android,password,android
来源: https://www.cnblogs.com/yclxt/p/16466073.html

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

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

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

ICode9版权所有