一、设计要求
上次仅完成微信界面的初始化和微信界面的切换功能,这次在上次作业的基础上增加列表项的单项点击功能,实现最新的activityforresult功能。
二、设计步骤
1.新建一个新的activity1,recycleview的某一项点击后跳转到这个新的activity1
activity_1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Activity1">
<TextView
android:id="@+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="344dp"
android:gravity="center"
android:text="具体内容"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView11"
android:layout_width="0dp"
android:layout_height="95dp"
android:layout_marginTop="96dp"
android:gravity="center"
android:text="聊天记录"
android:textSize="45dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看聊天记录"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.845" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity1.java
package com.example.myapplication;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class Activity1 extends AppCompatActivity {
///private Intent intent1;
private Button button;
private TextView textView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_1);
textView=findViewById(R.id.textView10);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Activity1.this,resultActivity.class);
test.launch(intent);
}
});
//Intent intent=getIntent();
}
public ActivityResultLauncher test= registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode()== Activity.RESULT_OK){
Log.d("lyr","onActivityResult 有返回值。。。");
//TextView textView=findViewById(R.id.textView10);
textView.setText(result.getData().getStringExtra("data"));
//textView2.setText(result.getData().getStringExtra("data"));
};
}
});
@Override
protected void onStart() {
super.onStart();
Log.d("lyr","activity2 is onStart...");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lyr","activity2 is onRestart...");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lyr" ,"activity2 is onStop...");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lyr","activity2 is onDestroy...");
}
@Override
public void finish(){
super.finish();
Log.d("lyr","activity2 is finish...");
}
@Override
public void finishActivity(int requestCode){
super.finishActivity(requestCode);
Log.d("lyr","finishActivity2...");
}
}
2.新建一个新的resultActivity,button点击后跳转到resultActivity
resultActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Calendar;
public class resultActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent =getIntent();
intent.putExtra("data","今天是肖蓉老师的安卓课。\n"+
Calendar.getInstance().getTime());
setResult(Activity.RESULT_OK,intent);
finish();
//String str1 = intent.getStringExtra("data");
//textView=findViewById(R.id.textView12);
//textView.setText(str1);
}
}
代码仓库:app/src/main · li/android - 码云 - 开源中国 (gitee.com)
标签:layout,parent,app,import,界面,android,void,类微信 From: https://www.cnblogs.com/Li12138/p/16814981.html