activity带参跳转和界面登录

首先

首先是MainActivity的xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7 
 8     tools:context="com.example.aaaaa.testliandong.MainActivity">
 9 
10 
11     <RelativeLayout
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content">
14 
15 
16         <TextView
17             android:id="@+id/username"
18             android:layout_width="wrap_content"
19             android:layout_height="wrap_content"
20             android:layout_marginTop="8dp"
21             android:text="用户名"
22             android:textSize="20sp" />
23 
24         <EditText
25             android:id="@+id/inputUserName"
26             android:layout_width="match_parent"
27             android:layout_height="wrap_content"
28             android:layout_marginTop="8dp"
29 
30             android:layout_toRightOf="@id/username"
31             android:hint="请输入用户名" />
32 
33 
34         <TextView
35             android:id="@+id/password"
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content"
38             android:layout_alignRight="@id/username"
39             android:layout_below="@id/inputUserName"
40             android:text="密 码"
41 
42             android:textSize="20sp" />
43 
44         <EditText
45             android:id="@+id/inputPassword"
46             android:layout_width="match_parent"
47             android:layout_height="wrap_content"
48 
49             android:layout_alignLeft="@id/inputPassword"
50             android:layout_below="@id/inputUserName"
51             android:layout_toRightOf="@id/password"
52             android:hint="请输入密码"
53             android:inputType="textPassword" />
54 
55     </RelativeLayout>
56 
57     <Button
58         android:id="@+id/btn"
59         android:layout_width="match_parent"
60         android:layout_height="wrap_content"
61         android:layout_marginTop="8dp"
62         android:text="登录" />
63     <!--显示传回来的东西-->
64     <TextView
65         android:id="@+id/tv"
66         android:layout_width="wrap_content"
67         android:layout_height="wrap_content"
68         android:layout_gravity="center_horizontal"
69         android:layout_marginTop="20dp"
70         android:text="显示回传的参数"
71         android:textSize="20sp" />
72 
73 </LinearLayout>

下面是MainActivity的代码

 1 package com.example.aaaaa.testliandong;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 import android.widget.TextView;
10 import android.widget.Toast;
11 
12 public class MainActivity extends AppCompatActivity {
13     private EditText user;
14     private EditText password;
15     private Button button;
16     private TextView tv;
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         button=(Button)findViewById(R.id.btn);
22         user=(EditText)findViewById(R.id.inputUserName);
23         password=(EditText)findViewById(R.id.inputPassword);
24         tv=(TextView)findViewById(R.id.tv);
25         button.setOnClickListener(new View.OnClickListener() {
26             @Override
27             public void onClick(View v) {
28                 //比较相等要用equals方法
29                 if(user.getText().toString().trim().equals("范冰冰")&&password.getText().toString().trim().equals("123")
30                         ||user.getText().toString().trim().equals("柳岩")&&password.getText().toString().trim().equals("123")){
31                     Intent intent=new Intent(MainActivity.this,NewActivity.class);
32 //                    startActivity(intent);
33 //                  另一种启动activity的方法 参数一:intent对象  参数二:requestCode传出的参数
34                     startActivityForResult(intent,10);
35                     Toast.makeText(MainActivity.this,"恭喜"+user.getText().toString()+"登录成功",Toast.LENGTH_SHORT).show();
36                 }else {
37                     Toast.makeText(MainActivity.this,"输入密码错误",Toast.LENGTH_SHORT).show();
38                 }
39             }
40         });
41     }
42 
43     @Override
44     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
45         super.onActivityResult(requestCode, resultCode, data);
46         //requestCode  resultCode中的两个参数分别是startActivityForResult的第二个参数和setResult的第一个参数
47         if (requestCode==10&&resultCode==12){
48             tv.setText(data.getStringExtra("name"));
49         }
50     }
51 }

下面是第二个界面的xml文件

 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     <TextView
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="嘿嘿,加油"
11         android:textSize="30sp" />
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="结束当前页面"
17         android:id="@+id/btn1"/>
18 </LinearLayout>

下面是第二个界面中的Java文件

 1 package com.example.aaaaa.testliandong;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 
 9 /**
10  * Created by 13320 on 2020/2/23.
11  */
12 public class NewActivity extends Activity {
13     private Button btn1;
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.newactivity);
17         btn1=(Button)findViewById(R.id.btn1);
18         btn1.setOnClickListener(new View.OnClickListener() {
19             @Override
20             public void onClick(View v) {
21                 //Intent空参
22                 Intent i=new Intent();
23                 //putExtra是以键值对的形式传入数据,我们会在主活动中的textView显示   恭喜你又成功了
24                 i.putExtra("name","恭喜你又成功了");
25                 //setResult 参数1:设置回传的数据 参数二设置intent对象
26                 setResult(12, i);
27                 //finish直接把当前activity结束掉
28                 finish();
29             }
30         });
31     }
32 }

当然因为使用了两个活动所以必不可少的就是要在AndroidMainifest中声明活动

功能:输入用户名,密码可进入

    把第二个界面中的数据传回第一个界面中,并且显示在TextView中

 

posted @ 2020-02-23 18:01  东功  阅读(557)  评论(0)    收藏  举报