一、案例演示
信息页面与状态页面进行信息交互,点击学习状态,跳转到学习状态页面,状态页面显示当前的状态,选择其他状态后,在跳回到我的信息页面,更改学习状态
information_activity_main.xml
status_activity.xml
二、实现步骤
1、status_activity.xml
学习状态页面
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/hui"
android:orientation="vertical"
tools:context=".StatusActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<ImageView
android:id="@+id/picture"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/left"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="73dp"
android:text="学习状态"
android:textSize="40dp"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/left2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="当前状态:"
android:textSize="25dp"></TextView>
<TextView
android:id="@+id/being"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/left2"
android:textSize="25dp">
</TextView>
</RelativeLayout>
<TextView style="@style/line"></TextView>
<RelativeLayout
android:id="@+id/cold"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/v1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image1"
android:text="生病中"
android:textSize="30dp"></TextView>
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/yao2" />
</RelativeLayout>
<TextView style="@style/line"></TextView>
<RelativeLayout
android:id="@+id/vacation"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/v2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image2"
android:text="度假中"
android:textSize="30dp"></TextView>
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/dujia"></ImageView>
</RelativeLayout>
<TextView style="@style/line"></TextView>
<RelativeLayout
android:id="@+id/study"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/v3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image3"
android:text="学习中"
android:textSize="30dp"></TextView>
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/time"></ImageView>
</RelativeLayout>
<TextView style="@style/line"></TextView>
<RelativeLayout
android:id="@+id/activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/v4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image4"
android:text="活动中"
android:textSize="30dp"></TextView>
<ImageView
android:id="@+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/tree"></ImageView>
</RelativeLayout>
<TextView style="@style/line"></TextView>
<RelativeLayout
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/v5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:text="无"
android:textSize="30dp"></TextView>
</RelativeLayout>
<TextView style="@style/line"></TextView>
</LinearLayout>
</LinearLayout>
2、StatusActivity.java
public class StatusActivity extends AppCompatActivity implements View.OnClickListener {
//图片箭头
private ImageView picture;
//生病
private RelativeLayout cold;
//假期
private RelativeLayout vacation;
//学习
private RelativeLayout study;
//活动
private RelativeLayout activity;
//无
private RelativeLayout no;
//现在状态
private TextView being;
private Intent intent = new Intent();
private Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status_activity);
//初始化视图
initView();
//获取传来的数据
Intent intent1 = getIntent();
//获得传来的bundle对象
Bundle bundle = intent1.getExtras();
//获取传来的状态
String state = bundle.getString("state");
being.setText(state);
}
public void initView() {
//找到对应控件
picture = findViewById(R.id.picture);
cold = findViewById(R.id.cold);
vacation = findViewById(R.id.vacation);
study = findViewById(R.id.study);
activity = findViewById(R.id.activity);
being = findViewById(R.id.being);
no = findViewById(R.id.no);
//设置监听
picture.setOnClickListener(this);
cold.setOnClickListener(this);
vacation.setOnClickListener(this);
study.setOnClickListener(this);
activity.setOnClickListener(this);
no.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//点击对应按钮传递对应数据
switch (v.getId()) {
case R.id.picture:
finish();break;
case R.id.cold:
bundle.putString("cold", "生病中");
bundle.putInt("image", R.drawable.yao2);
break;
case R.id.vacation:
bundle.putString("cold", "度假中");
bundle.putInt("image", R.drawable.dujia);
break;
case R.id.study:
bundle.putString("cold", "学习中");
bundle.putInt("image", R.drawable.time);
break;
case R.id.activity:
bundle.putString("cold", "活动中");
bundle.putInt("image", R.drawable.tree);
break;
case R.id.no:
bundle.putString("cold", "无");
break;
default:
break;
}
//打包存储到intent中
intent.putExtras(bundle);
//返回结果码-1
setResult(RESULT_OK, intent);
//销毁当前页面
finish();
}
}
3、InformationActivity.java
点击选择学习状态执行text11()方法
//选择状态
public void text11() {
//获取现在的状态,在view11中
String now = view11.getText().toString().trim();
Intent intent = new Intent(InformationActivity.this, StatusActivity.class);
//创建存储组
Bundle bundle = new Bundle();
bundle.putString("state", now);
intent.putExtras(bundle);
//跟随页面跳转带到下一个页面
startActivityForResult(intent, 1);
}
//onActivityResult接收数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
//获取数据
Bundle bundle = data.getExtras();
String nowState = bundle.getString("cold");
int imageId = bundle.getInt("image");
//点击了生病,把获取的值赋值给,当前状态
view11.setText(nowState);
// imageview.setBackground(getResources().getDrawable(imageId));
}
}
}