实验3.1 简单实现Intent带返回值的跳转
在这里实现的功能是当点击listview的某一子项是,启动一个确认删除相应记录的子Activity。
所以用到了带返回值的intent
父界面和子界面的前台代码
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical">
<ListView
android:layout_marginLeft="10dp"
android:id="@+id/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
activity_new
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".newactivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="删除"
android:textSize="20dp"/>
<TextView android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"/>
<TextView
android:id="@+id/t3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="的记录吗"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="确定"/>
<Button android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
对应的后台代码
package com.example.hndx.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String a;//全局变量记录点击的哪一项
ListView listView;
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.l1 );
List<String>list=new ArrayList<String>();
list.add("移动计算");
list.add("计算机组成原理");
list.add("数据结构");
list.add("操作系统");
list.add("面向对象技术");
list.add("逻辑设计");
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
AdapterView.OnItemClickListener listViewListener =new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {//i表示子项的位置,l表示子项的行号
String mag=adapter.getItem(i);
a=mag;
final Intent intent =new Intent(MainActivity.this,newactivity.class);
intent.putExtra("name",mag.toString());
startActivityForResult(intent,1);
}
};
listView.setOnItemClickListener(listViewListener);
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){//requestCode 为父界面的编号
//resultCode 为子界面的返回编号
//data可以使传递的值
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==1&&resultCode==2 ){
adapter.remove(a);
}
}
}
package com.example.hndx.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class newactivity extends AppCompatActivity {
TextView t1,t2,t3;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
b1=(Button) findViewById(R.id.b1);
b2=(Button) findViewById(R.id.b2);
t1=(TextView) findViewById(R.id.t1);
t2=(TextView) findViewById(R.id.t2);
t3=(TextView) findViewById(R.id.t3);
final Intent intent=getIntent();
String name=intent.getStringExtra("name");
t1.setText(name);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent();
setResult(2,intent);//设置子界面的编号并返回
finish();
}
});
b2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
newactivity.this.finish();
}
});
// t2.setText("123");
}
}