0
点赞
收藏
分享

微信扫一扫

【Android 练习】LsitView、Intent携带数据回跳


启动一个 Activity,并获得该 Activity 返回的结果

问题描述:

  • 程序包含两个 Activity,其中一个 Activity 中包含一个按钮和文本框,按钮内
    容为“请选择一个学历”,点击该按钮,弹出另一个 Activity,该 Activity 中显示一个学
    历列表,选择某一学历后,该学历名称将显示在前一个 Activity 的文本框中。运行效果如
    图 1 所示。
  • 【Android 练习】LsitView、Intent携带数据回跳_android

程序逻辑要求:
(1)使用 startActivityForResult 方法实现该功能。学历列表使用 ArrayAdapter 绑定
ListView 实现;“资源类”中存放有字符串资源文件和 MainActivity 的界面描述文件;

(2)考生按照系统提示目录保存试题文件,每道试题建立一个文件夹,文件夹名为题目编
号。该试题文件夹名为“1”,该文件夹内应包含 6 个文件,分别为:
项目打包压缩文件:App1.zip
第一个 Activity 的 Java 文件:MainActivity.java
第二个 Activity 的 Java 文件:SelectActivity.java
界面截图(选择学历前):1.png
界面截图(选择学历中):2.png
界面截图(选择学历后):3.png

解析:

【Android 练习】LsitView、Intent携带数据回跳_android_02

代码如下:

EducationActivity.xml

【Android 练习】LsitView、Intent携带数据回跳_xml_03

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.EducationActivity">
<Button
android:id="@+id/education"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="51dp"
android:text="请选择学历" />
<EditText
android:id="@+id/education_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="54dp"
android:layout_marginEnd="56dp"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</RelativeLayout>

ListActivity.xml

【Android 练习】LsitView、Intent携带数据回跳_android_04

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.ListActivity">
<ListView
android:id="@+id/education_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="112dp"
tools:layout_editor_absoluteY="116dp" />
</LinearLayout>

ListActivity .java

package com.example.learning.Activity;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.learning.R;

public class ListActivity extends AppCompatActivity {

// 声明组件
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

// 获取组件
listView = findViewById(R.id.education_item);
// 模拟数据
String[] choose = new String[]{"小学","中学","高职专科","本科","研究生","博士"};
// 添加适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,choose);
listView.setAdapter(adapter);
// 添加事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 回退的数据
String back = choose[position];
Toast.makeText(getApplicationContext(),back,Toast.LENGTH_SHORT).show();
// 创建Intent跳转
Intent intent = new Intent();
intent.putExtra("backResult",back);
// 设置返回结果
setResult(2,intent);
// 关闭当前Activity
finish();
}
});

}
}

EducationActivity .java

package com.example.learning.Activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.learning.R;

public class EducationActivity extends AppCompatActivity {

// 声明组件
Button education;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_education);

// 获取组件
education = findViewById(R.id.education);

// 按钮点击事件
education.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent跳转
Intent intent = new Intent(EducationActivity.this,ListActivity.class);
startActivityForResult(intent,1);
}
});
}

// 重写 onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 判断请求码和结果码
if (requestCode ==1 && resultCode ==2){
// 获取返回的结果
String resultStr = data.getStringExtra("backResult");
// 将返回的结果放入文本框中
//Toast.makeText(getApplicationContext(),resultStr,Toast.LENGTH_SHORT).show();
EditText education_txt = findViewById(R.id.education_txt);
education_txt.setText(resultStr);
}
}
}

【Android 练习】LsitView、Intent携带数据回跳_android_05


举报

相关推荐

0 条评论