简单介绍一下Android界面之间的传值,以下为效果展示图
1:
2:
3:
以下为代码示例
activity_main.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"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.kongjian_10.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:id="@+id/et_main_text"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写心情"
android:onClick="writerHeart"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_main_showThank"
/>
</LinearLayout>
activity_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_item_showName"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="5"
android:hint="请输入心情"
android:gravity="top"
android:id="@+id/et_item_text"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="ok"
android:text="输入完毕"
/>
</LinearLayout>
MainActivity.java
package com.example.kongjian_10;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText et;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et_main_text);
tv = (TextView) findViewById(R.id.tv_main_showThank);
String content=getIntent().getStringExtra("content");
tv.setText(content);
}
public void writerHeart(View view){
String uname=et.getText().toString();
Intent intent=new Intent(this,ItemActivity.class);
intent.putExtra("uname",uname);
startActivity(intent);
}
}
ItemActivity.java
package com.example.kongjian_10;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by Administrator on 2017/6/16.
*/
public class ItemActivity extends AppCompatActivity {
private TextView tv;
private EditText et;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
tv = (TextView) findViewById(R.id.tv_item_showName);
et = (EditText) findViewById(R.id.et_item_text);
//获取第一个界面传过来的数据
String uname=getIntent().getStringExtra("uname");
tv.setText(uname);
}
public void ok(View view){
String content=et.getText().toString();
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("content",content);
startActivity(intent);
}
}