MainActivity5.java
package com.example.myapplication_one;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity5 extends AppCompatActivity implements View.OnClickListener {
private EditText editText1;
private EditText editText2;
private Button button1, button2;
ImageView imageView1,imageView2;
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//用于加载页面布局 R.layout.名称
setContentView(R.layout.layout5);
init();
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
private void init() {
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
imageView1 = findViewById(R.id.imageView1);
imageView2 = findViewById(R.id.imageView2);
textView1 = findViewById(R.id.textView1);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
//点击button1时的操作
//context:上下文,text:显示的文本的信息,duration:显示时间
String name1 = editText1.getText().toString();
String name2 = editText2.getText().toString();
/*
* Intent:意图,显式启动 ———— 隐式启动
* Intent intent = new Intent(PackageContext,cls);
* PackageContext :上下文
* cls:想要启动的目标活动
*/
//显式启动Intent
//Intent intent = new Intent(MainActivity3.this,MainActivity4.class);
//startActivity(intent);
if(name1.equals("admin") && name2.equals("123")){
imageView1.setImageResource(R.drawable.ic_launcher_background);
Intent intent = new Intent(MainActivity5.this,MainActivity4.class);
intent.putExtra("UserName", name1);//键值对
intent.putExtra("Password" ,name2);
//startActivity(intent);
startActivityForResult(intent,1001);//1001表示请求码
}else{
Toast.makeText(MainActivity5.this, "你输入的用户信息不正确", Toast.LENGTH_LONG).show();
editText1.setText("");
editText2.setText("");
}
break;
case R.id.button2:
Toast.makeText(MainActivity5.this, "点击button按钮2", Toast.LENGTH_LONG).show();
imageView2.setImageResource(R.drawable.ic_launcher_background);
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity5.this);
dialog.setTitle("Dialog");
dialog.setMessage("是否进行注册");
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});//积极的按钮
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});//消极的按钮
dialog.show();
break;
default:
break;
}
}
//onActivityResult()方法,作用接受第二活动传递的数据
//int requestCode:请求码,startActivityForResult(intent,1001);
//int resultCode:结果码, setResult(1002,intent);
//Intent data :第二个页面封装的数据,setResult(1002,intent);
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
switch (requestCode){
case 1001:
if(resultCode==1002){
String info = data.getStringExtra("info");
textView1.setText(info);
}
break;
}
}
}
layout5
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:text="@string/User" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_toRightOf="@id/textView1"
android:hint="请输入用户名"
android:maxLines="2"
android:ems="10">
<requestFocus/>
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="27dp"
android:text="@string/Password" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@id/textView2"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="233dp"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="88dp"
android:layout_marginTop="46dp"
android:layout_marginRight="89dp"
android:textAllCaps="true"
android:text="登录" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="88dp"
android:layout_marginTop="270dp"
android:layout_marginRight="89dp"
android:background="#ff0000"
android:gravity="center"
android:text="TextView"
android:textColor="#00ff00"
android:textSize="35sp" />
<Button
android:id="@+id/button2"
android:layout_width="233dp"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="88dp"
android:layout_marginTop="100dp"
android:layout_marginRight="89dp"
android:textAllCaps="true"
android:text="注册" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:layout_marginLeft="68dp"
android:layout_marginTop="350dp"
app:srcCompat="@mipmap/f1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="92dp"
android:layout_marginLeft="228dp"
android:layout_marginTop="350dp"
app:srcCompat="@mipmap/f2" />
</RelativeLayout>
MainActivity4.java
package com.example.myapplication_one;
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.TextView;
public class MainActivity4 extends AppCompatActivity {
TextView textView1,textView2;
Button button1;
EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//用于加载页面布局 R.layout.名称
setContentView(R.layout.layout4);
init();
Intent intent = getIntent();//getIntent();获取当前intent对象
String userName = intent.getStringExtra("UserName");
String password = intent.getStringExtra("Password");
textView1.setText(userName+password);
textView2.setText(userName+password);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
//使用Intent对象封装数据
intent.putExtra("info",editText1.getText().toString());
//将数据返回到第一个页面
setResult(1002,intent);
finish();//关闭当前的Activity(关闭第二个页面,回到第一个页面)
}
});
}
private void init(){
textView1 = (TextView) findViewById(R.id.textView2);
textView2 = (TextView) findViewById(R.id.textView3);
button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
}
}
layout4
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#00ff00"
android:gravity="center"
android:text="第二个活动"
android:textColor="@color/purple_200"
android:textSize="25dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="70dp"
android:layout_weight="1"
android:text="Username" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="40dp"
android:layout_weight="1"
android:ems="10" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="120dp"
android:layout_weight="1"
android:text="Password" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:layout_marginLeft="120dp"
android:layout_marginTop="100dp"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="160dp"
android:text="返回数据" />
</RelativeLayout>