- 浏览: 4172958 次
最新评论
intent传值
今天我们来探讨一下android的传值问题。
主要实现功能为第一个页面实现信息的填写,在第二个页面实现第一个页面信息的输出
效果图为:
第一个activity实现了对单选、复选、文本框值的获取与传递
- ////////////////////UIZuoYeActivity///////////////
- //第一个activity
- packagecn.class3g.activity;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.CheckBox;
- importandroid.widget.EditText;
- importandroid.widget.RadioButton;
- importandroid.widget.RadioGroup;
- importandroid.widget.Spinner;
- publicclassUIZuoYeActivityextendsActivityimplementsOnClickListener{
- /**Calledwhentheactivityisfirstcreated.*/
- RadioGrouprg=null;
- RadioButtonmanRB=null;
- RadioButtonrb=null;
- Buttonbtn=null;
- EditTextnameET=null;
- CheckBoxlan,zu,pai,ping;
- Spinnercity;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findView();
- }
- privatevoidfindView(){
- btn=(Button)this.findViewById(R.id.putinId);
- nameET=(EditText)this.findViewById(R.id.nameId);
- manRB=(RadioButton)this.findViewById(R.id.manId);
- lan=(CheckBox)this.findViewById(R.id.lanId);
- zu=(CheckBox)this.findViewById(R.id.zuId);
- pai=(CheckBox)this.findViewById(R.id.paiId);
- ping=(CheckBox)this.findViewById(R.id.pingId);
- city=(Spinner)this.findViewById(R.id.cityId);
- btn.setOnClickListener(this);
- }
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- //封装bundle对象
- Bundlebundle=newBundle();
- //获取EditText文本框内容
- bundle.putString("name","用户名称:"+nameET.getText().toString());
- //获取RadioGroup单选内容
- if(manRB.isChecked()){
- bundle.putString("sex","性别:男");
- }else{
- bundle.putString("sex","性别:女");
- }
- //获取CheckBox复选框内容
- Stringtemp="爱好:";
- if(lan.isChecked()){
- temp+=lan.getText().toString();
- }
- if(zu.isChecked()){
- temp+="";
- temp+=zu.getText().toString();
- }
- if(pai.isChecked()){
- temp+="";
- temp+=pai.getText().toString();
- }
- if(ping.isChecked()){
- temp+="";
- temp+=ping.getText().toString();
- }
- bundle.putString("hobby",temp);
- //获取Spinner下拉菜单内容
- bundle.putString("city","城市:"+city.getSelectedItem().toString());
- Intentintent=newIntent(UIZuoYeActivity.this,PutInActivity.class);
- //传递
- intent.putExtras(bundle);
- startActivity(intent);
- }
- }
////////////////////UIZuoYeActivity/////////////// //第一个activity package cn.class3g.activity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; public class UIZuoYeActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ RadioGroup rg = null; RadioButton manRB = null; RadioButton rb = null; Button btn = null; EditText nameET = null; CheckBox lan, zu, pai, ping; Spinner city; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findView(); } private void findView() { btn = (Button) this.findViewById(R.id.putinId); nameET = (EditText) this.findViewById(R.id.nameId); manRB = (RadioButton) this.findViewById(R.id.manId); lan = (CheckBox) this.findViewById(R.id.lanId); zu = (CheckBox) this.findViewById(R.id.zuId); pai = (CheckBox) this.findViewById(R.id.paiId); ping = (CheckBox) this.findViewById(R.id.pingId); city = (Spinner) this.findViewById(R.id.cityId); btn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub // 封装bundle对象 Bundle bundle = new Bundle(); // 获取EditText文本框内容 bundle.putString("name", "用户名称:" + nameET.getText().toString()); // 获取RadioGroup单选内容 if (manRB.isChecked()) { bundle.putString("sex", "性别:男"); } else { bundle.putString("sex", "性别:女"); } // 获取CheckBox复选框内容 String temp = "爱好:"; if (lan.isChecked()) { temp += lan.getText().toString(); } if (zu.isChecked()) { temp += ""; temp += zu.getText().toString(); } if (pai.isChecked()) { temp += ""; temp += pai.getText().toString(); } if (ping.isChecked()) { temp += ""; temp += ping.getText().toString(); } bundle.putString("hobby", temp); // 获取Spinner下拉菜单内容 bundle.putString("city", "城市:" + city.getSelectedItem().toString()); Intent intent = new Intent(UIZuoYeActivity.this, PutInActivity.class); // 传递 intent.putExtras(bundle); startActivity(intent); } }
第一个界面实现了整体布局
- //////////////////////main.xml///////////////////
- //第一个界面
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/title"/>
- <TableLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:stretchColumns="1">
- <TableRow>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/name"/>
- <EditText
- android:id="@+id/nameId"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </TableRow>
- <TableRow>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="@string/sex"/>
- <RadioGroup
- android:id="@+id/sexId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:checkedButton="@+id/manId"
- android:orientation="horizontal">
- <RadioButton
- android:id="@id/manId"
- android:text="男"/>
- <RadioButton
- android:id="@+id/womanId"
- android:text="女"/>
- </RadioGroup>
- </TableRow>
- <TableRow>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="@string/city"/>
- <Spinner
- android:id="@+id/cityId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:entries="@array/城市"
- android:prompt="@string/city"/>
- </TableRow>
- <TableRow>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="@string/hobby"/>
- <TableLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:stretchColumns="1">
- <TableRow>
- <CheckBox
- android:id="@+id/lanId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="篮球"/>
- <CheckBox
- android:id="@+id/zuId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="足球"/>
- </TableRow>
- </TableLayout>
- </TableRow>
- <TableRow>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TableLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:stretchColumns="1">
- <TableRow>
- <CheckBox
- android:id="@+id/paiId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="排球"/>
- <CheckBox
- android:id="@+id/pingId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="乒乓球"/>
- </TableRow>
- </TableLayout>
- </TableRow>
- </TableLayout>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:id="@+id/putinId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="@string/putin"/>
- </RelativeLayout>
- </LinearLayout>
//////////////////////main.xml/////////////////// //第一个界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title" /> <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="1" > <TableRow > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/name" /> <EditText android:id="@+id/nameId" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/sex" /> <RadioGroup android:id="@+id/sexId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checkedButton="@+id/manId" android:orientation="horizontal" > <RadioButton android:id="@id/manId" android:text="男" /> <RadioButton android:id="@+id/womanId" android:text="女" /> </RadioGroup> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/city" /> <Spinner android:id="@+id/cityId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/城市" android:prompt="@string/city" /> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/hobby" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="1" > <TableRow > <CheckBox android:id="@+id/lanId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球" /> <CheckBox android:id="@+id/zuId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球" /> </TableRow> </TableLayout> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="1" > <TableRow > <CheckBox android:id="@+id/paiId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="排球" /> <CheckBox android:id="@+id/pingId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="乒乓球" /> </TableRow> </TableLayout> </TableRow> </TableLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/putinId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/putin" /> </RelativeLayout> </LinearLayout>
第二个activity实现了对第一个界面输入内容的获取,获取到内容后,然后List迭代,用ListView在第二个界面上显示出来
- //////////////////PutInActivity///////////////////
- //第二个activity
- packagecn.class3g.activity;
- importjava.util.ArrayList;
- importjava.util.List;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.ArrayAdapter;
- importandroid.widget.Button;
- importandroid.widget.ListView;
- publicclassPutInActivityextendsActivity{
- ListViewlistView=null;
- Bundlebundle;
- //TextViewlistView=null;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.putin);
- listView=(ListView)findViewById(R.id.listId);
- //接收
- bundle=this.getIntent().getExtras();
- List<String>list=newArrayList<String>();
- list.add(bundle.getString("name"));
- list.add(bundle.getString("sex"));
- list.add(bundle.getString("city"));
- list.add(bundle.getString("hobby"));
- ArrayAdapter<String>Adapter=newArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1,list);
- listView.setAdapter(Adapter);
- Buttonbtn=(Button)this.findViewById(R.id.returnId);
- btn.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- setContentView(R.layout.main);
- }
- });
- }
- }
//////////////////PutInActivity/////////////////// //第二个activity package cn.class3g.activity; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class PutInActivity extends Activity { ListView listView = null; Bundle bundle; // TextView listView = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.putin); listView = (ListView) findViewById(R.id.listId); // 接收 bundle = this.getIntent().getExtras(); List<String> list = new ArrayList<String>(); list.add(bundle.getString("name")); list.add(bundle.getString("sex")); list.add(bundle.getString("city")); list.add(bundle.getString("hobby")); ArrayAdapter<String> Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); listView.setAdapter(Adapter); Button btn = (Button) this.findViewById(R.id.returnId); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub setContentView(R.layout.main); } }); } }
- //////////////////////putin.xml/////////////////////
- //第二个界面
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/title"/>
- <ListView
- android:id="@+id/listId"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/returnId"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="返回"/>
- </LinearLayout>
//////////////////////putin.xml///////////////////// //第二个界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title" /> <ListView android:id="@+id/listId" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/returnId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" /> </LinearLayout>
相关推荐
android activityIntent 传值 传对象
Activity生命周期及Intent传值
本代码为博客代码,博客地址:http://blog.csdn.net/zhangjinhuang
支持动态增加的listview,同时支持intent传值,使用putextra将数值传递给另一个activity,另一activity通过hasextra补捉到获取的intent数值
Android Intent多种传值方式 https://blog.csdn.net/qq_31939617/article/details/80742103
举个例子我现在要从A界面跳转到B界面或者C界面 这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两遍添加值的方法 那么 如果我用1个Bundle 直接把值先存里边 然后再存到Intent中 不就更...
实现Intent跳转及传值.用Intent方式建立两个Activity页面如下
Activity之间使用intent(可以是Bundle)传值和回传值的demo
自制的ListView数据单击后,Intent跳转到另一个界面,还运用到了ToggleButton Toast SimpleAdapter HashMap 互相学习,顺便赚点分数更加好好学习 祝大家一起成功
只有一个活动的应用也太简单了吧?没错,你的追求应该更高一点。不管你想创建多少 个活动,方法都和上一节中介绍的是一样的。...标只会进入到该应用的主活动,那么怎样才能由主活动跳转到其他活动呢?...
Intent跳转传值,和Activity返回传值
文档+代码,android Application全局传值替代Intent传值,解决了Intent传值的限制
Intent 数据传递,包含基本数据类型,对象类型,包含基本数据类型的数组,包含对象类型的数组,涉及到Parceable接口 Seriziable接口的用法
用Intent进行跳转并传值给下一个Activity.
Intent,又称为意图,是一种运行时绑定机制,它能在程序运行的过程中链接两个不同的组件(Activity、Service、BroadcastReceiver)。通过Intent,程序可以向Android表达某种请求或意愿,Android会根据意愿的内容选择...
Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用
Xamarin.Android之Intent传递对象简单实例,实现的是IParcelable接口
自己项目中用到fragment搭建框架,从网上找得一个demo,实现activity与fragment相互传值;fragment与fragment跳转。跟大家分享