最近想做一个设置按钮,完事后返回。
先上大概效果:
实现过程:
1、在activity_main中添加一个设置按钮和一个文本显示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="293dp"
android:layout_height="wrap_content"
android:layout_marginStart="700dp"
android:layout_marginTop="205dp"
android:text="接收返回值"
android:textColor="@color/black"
android:textSize="45sp" />
<Button
android:id="@+id/button"
android:layout_width="161dp"
android:layout_height="75dp"
android:layout_marginStart="500dp"
android:layout_marginTop="200dp"
android:text="设置" />
</FrameLayout>
2、在layout中添加一个activity_setting.xml,添加一个返回按钮
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="161dp"
android:layout_height="75dp"
android:layout_marginStart="100dp"
android:layout_marginTop="100dp"
android:text="返回" />
</FrameLayout>
3、在AndroidManifest.xml中注册?.SettingActivity
4、添加SettingActivity类。
编辑MainActivity.java和SettingActivity.java
package com.example.jumptest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
final static int REQUEST=10;
private Button btnSetting;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSetting=(Button) findViewById(R.id.button);
tv = (TextView) findViewById(R.id.textView2);
// 对按钮添加点击事件的监听
btnSetting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SettingActivity.class);
startActivityForResult(intent, REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST) {
if (resultCode == RESULT_OK) {
tv.setText("成功");
}else if (resultCode == RESULT_CANCELED) {
tv.setText("失败");
}
}
}
}
package com.example.jumptest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class SettingActivity extends AppCompatActivity {
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent data = new Intent();
setResult(RESULT_OK, data);
finish();
}
});
}
}
|