Android-登录测试
作者:互联网
第一步:做一个简单的UI登录界面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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