acrivity_main.xml
< ? xml version= "1.0" encoding= "utf-8" ? >
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: app= "http://schemas.android.com/apk/res-auto"
xmlns: tools= "http://schemas.android.com/tools"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
android: orientation= "vertical"
tools: context= "com.example.uitest.MainActivity" >
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: orientation= "horizontal" >
< TextView
android: layout_width= "0dp"
android: layout_height= "wrap_content"
android: text= "用户名:"
android: textSize= "30sp"
android: textColor= "#a5c1c0"
android: layout_weight= "1" / >
< EditText
android: id= "@+id/edit_userName"
android: layout_width= "0dp"
android: layout_height= "wrap_content"
android: hint= "请输入用户名"
android: textSize= "30sp"
android: textColor= "#a5c1c0"
android: layout_weight= "2" / >
< / LinearLayout>
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "wrap_content" >
< TextView
android: layout_width= "0dp"
android: layout_height= "wrap_content"
android: text= "密 码:"
android: textSize= "30sp"
android: textColor= "#C0FF3E"
android: layout_weight= "1" / >
< EditText
android: id= "@+id/edit_password"
android: layout_width= "0dp"
android: layout_height= "wrap_content"
android: hint= "请输入密码"
android: textSize= "30sp"
android: textColor= "#C0FF3E"
android: layout_weight= "2" / >
< / LinearLayout>
< Button
android: id= "@+id/login"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: text= "登录"
android: textAllCaps= "false"
android: textColor= "#08ee00"
android: textSize= "30sp"
android: background= "@color/colorback"
/ >
< / LinearLayout>
activity_server.xml
< ? xml version= "1.0" encoding= "utf-8" ? >
< android. support. constraint. ConstraintLayout xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: app= "http://schemas.android.com/apk/res-auto"
xmlns: tools= "http://schemas.android.com/tools"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
tools: context= "com.example.uitest.ServerActivity" >
< TextView
android: id= "@+id/textLoginData"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: textColor= "#FFF166"
android: textSize= "30sp" / >
< / android. support. constraint. ConstraintLayout>
ServerActivity.java
package com. example. uitest;
import android. content. Intent;
import android. support. v7. app. AppCompatActivity;
import android. os. Bundle;
import android. widget. TextView;
public class ServerActivity extends AppCompatActivity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_server) ;
TextView textView= findViewById ( R. id. textLoginData) ;
Intent intent= getIntent ( ) ;
String userName= intent. getStringExtra ( "userName" ) ;
String password= intent. getStringExtra ( "password" ) ;
textView. setText ( "登录数据" + "用户为:" + userName+ "密码为:" + password) ;
}
}
MainActivity.java
package com. example. uitest;
import android. content. Intent;
import android. support. v7. app. AppCompatActivity;
import android. os. Bundle;
import android. view. View;
import android. widget. Button;
import android. widget. EditText;
import android. widget. Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_main) ;
final EditText editTextUerName= findViewById ( R. id. edit_userName) ;
final EditText editTextPassword= findViewById ( R. id. edit_password) ;
Button buttonLogin= findViewById ( R. id. login) ;
buttonLogin. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
String userName= editTextUerName. getText ( ) . toString ( ) ;
String password= editTextPassword. getText ( ) . toString ( ) ;
String mname= "你的名字" ;
String mpass= "12345678" ;
if ( userName. equals ( mname) && password. equals ( mpass) ) {
Intent intent= new Intent ( MainActivity. this , ServerActivity. class ) ;
intent. putExtra ( "userName" , userName) ;
intent. putExtra ( "password" , password) ;
startActivity ( intent) ;
Toast. makeText ( MainActivity. this , "登录成功" , Toast. LENGTH_LONG) . show ( ) ;
} else if ( "" . equals ( userName) && "" . equals ( password) ) {
Toast. makeText ( MainActivity. this , "用户名或密码不能为空" , Toast. LENGTH_LONG) . show ( ) ;
} else {
Toast. makeText ( MainActivity. this , "用户名或密码错误" , Toast. LENGTH_LONG) . show ( ) ;
}
}
} ) ;
}
}