1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/id_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/text_button"/>
<TextView
android:id="@+id/id_textview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="@android:style/TextAppearance.Holo.Large"
android:gravity="center"
/>
</LinearLayout>
2.strings.xml
<resources>
<string name="app_name">Ch0702</string>
<string name="text_button">点击</string>
<string name="text_empty">""</string>
</resources>
3.activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/id_iv01"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@mipmap/png0001"
android:contentDescription="@string/text_empty" />
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#aaa"/>
<ImageView
android:id="@+id/id_iv02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@mipmap/png0002"
android:contentDescription="@string/text_empty" />
</LinearLayout>
4.MainActivity.java
package com.example.ch0702;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static int RC01=1000;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//按钮等待被点击
Button btn=this.findViewById(R.id.id_button);
//等待被单击监听操作
btn.setOnClickListener(this);
tv=this.findViewById(R.id.id_textview);
}
@Override
public void onClick(View v){
//实现跳转操作 具体名字
Intent intent=new Intent(this,SecondActivity.class);
// 返回值(运行这个方法会启动下边onActivityResult操作)
this.startActivityForResult(intent,MainActivity.RC01);//划掉代表不建议使用
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
//判断返回值是否符合条件
if((requestCode==MainActivity.RC01)&&(resultCode==Activity.RESULT_OK)){
int which= data.getIntExtra("result",-1);
Log.i("myerror","123");
//让文本框显示对应的内容
tv.setText(which+"");
}
}
}
5.SecondActivity.java
package com.example.ch0702;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class SecondActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
//调用父类引用
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//通过id寻找view(模块)
ImageView iv01 = this.findViewById(R.id.id_iv01);
//设置监听单击操作
iv01.setOnClickListener(this);
ImageView iv02 = this.findViewById(R.id.id_iv02);
iv02.setOnClickListener(this);
}
@Override
//当检测到单击操作则会执行onClick操作
public void onClick(View v){
int id =v.getId();
//跳转命令,实现新界面
Intent intent=new Intent();//括号无内容代表模糊匹配
//判断获取id具体为哪一个id
if(id==R.id.id_iv01){
intent.putExtra("result",1);
}else {
intent.putExtra("result",2);
}//与mainactivity.java文件中的result相对应
this.setResult(Activity.RESULT_OK,intent);
this.finish();
}
}
6.AmdroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ch0702">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Ch0702">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:allowEmbedded="false">
</activity>
</application>
</manifest>
7.最后效果展示
video