Android实现Activity之间的传值【最简单的讲解+详细+源码+分析各种类型】
效果图:
MainActivity类:
package ccv.turbosnail.bo_intent_two;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText one,two,three;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); //初始化控件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getData(); //进行传值处理
}
});
}
private void getData() {
String one_string = one.getText().toString().trim(); //得到输入框的值
String two_string = two.getText().toString().trim();
String three_string = three.getText().toString().trim();
Intent intent = new Intent(MainActivity.this,TwoActivity.class); //参数一:所在类,参数二:要传递的类
// 单个变量传值
intent.putExtra("one",""+one_string); //这里加双引号值是为了让它被认为是字符串,其没用任何作用
intent.putExtra("two",""+two_string);
intent.putExtra("three",""+three_string);
// 传递数组
intent.putExtra("shuzu",new String[]{"上课","学习","玩游戏"});
//传递集合
ArrayList<String> list = new ArrayList<String>();
list.add("撩妹");
list.add("坚持");
list.add("到手");
intent.putStringArrayListExtra("jihe",list);
startActivity(intent); //启动intent
}
private void initView() {
one = findViewById(R.id.edt_one);
two = findViewById(R.id.edt_two);
three = findViewById(R.id.edt_three);
button = findViewById(R.id.btn_button);
}
}
TwoActivity 类:
package ccv.turbosnail.bo_intent_two;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
public class TwoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Intent intent = getIntent(); //得到传递过来的intent
// 接收单个值
String one = intent.getStringExtra("one"); //第一个参数是key,第二个参数是没有值时,默认值就为0
int one_int = Integer.parseInt(one); //将字符串转为整型
String two = intent.getStringExtra("two");
String three = intent.getStringExtra("three");
float three_float = Float.parseFloat(three); //将字符串转为浮点型
// 接收数组值
String[] shuzuArray = intent.getStringArrayExtra("shuzu");
String s = Arrays.toString(shuzuArray);
// 接受集合值
ArrayList<String > list = intent.getStringArrayListExtra("jihe");
String str = Arrays.toString(list.toArray());
TextView mone = findViewById(R.id.tv_one);
TextView mtwo = findViewById(R.id.tv_two);
TextView mthree = findViewById(R.id.tv_three);
TextView mfour = findViewById(R.id.tv_four);
TextView mfive = findViewById(R.id.tv_five);
mone.setText(""+one_int); //这里加双引号值是为了让它被认为是字符串,其没用任何作用
mtwo.setText(""+two);
mthree.setText(""+three_float);
mfour.setText(""+s);
mfive.setText(""+str);
}
}
布局代码activity_main:
<?xml version="1.0" encoding="utf-8"?><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15dp"
android:text="1.请问你今年几岁了?"/>
<EditText
android:id="@+id/edt_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#03bbfd"
android:textColor="#fff"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:hint="请输入年龄"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="10dp"
android:textSize="15dp"
android:text="2.请大声告诉我你喜欢的女孩是谁?"/>
<EditText
android:id="@+id/edt_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#03bbfd"
android:textColor="#fff"
android:textSize="20dp"
android:hint="请输入你喜欢的女孩"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="20dp"
android:textSize="15dp"
android:text="3. 52.06 % 10 = ?"/>
<EditText
android:id="@+id/edt_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="#fff"
android:background="#03bbfd"
android:textSize="20dp"
android:hint="请输入答案"/>
<Button
android:id="@+id/btn_button"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:background="#03bbfd"
android:layout_gravity="center"
android:textSize="20dp"
android:text="提交"/>
布局代码:activity_Two:
<?xml version="1.0" encoding="utf-8"?><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15dp"
android:text="1.请问你喜欢吃什么?"/>
<TextView
android:id="@+id/tv_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#03bbfd"
android:textColor="#fff"
android:textSize="18dp"
android:padding="10dp"
android:text="请输入年龄"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="10dp"
android:textSize="15dp"
android:text="2.请大声告诉我你喜欢的女孩是谁?"/>
<TextView
android:id="@+id/tv_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="10dp"
android:background="#03bbfd"
android:textColor="#fff"
android:textSize="18dp"
android:text="请输入你喜欢的女孩"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="10dp"
android:textSize="15dp"
android:text="3. 52.06 % 10 = ?"/>
<TextView
android:id="@+id/tv_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#03bbfd"
android:textColor="#fff"
android:padding="10dp"
android:textSize="18dp"
android:text="请输入答案"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="10dp"
android:textSize="15dp"
android:text="4.数组是:"/>
<TextView
android:id="@+id/tv_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#03bbfd"
android:textColor="#fff"
android:padding="10dp"
android:textSize="18dp"
android:text="请输入答案"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="10dp"
android:textSize="15dp"
android:text="5. 集合是:"/>
<TextView
android:id="@+id/tv_five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#03bbfd"
android:textColor="#fff"
android:padding="10dp"
android:textSize="18dp"
android:text="请输入答案"/>
如果觉得我写的好的话,请关注我,你们的每一个关注,是我努力写下去最大的动力,也是最大的欣慰。谢谢你们的喜欢。!我们一起要加油啊!!!