初学Android,这是一个用户登录界面,想把里面的值传递到另一个Activity中,先不说其他,上代码:
主界面(登录界面的Activity):MainActivity.java
package com.zhoujunwen.widget; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.content.Intent; import android.view.View.OnClickListener; import android.widget.*; public class MainActivity extends Activity { //声明按钮Button private Button register,cancle; //声明ToggleButton private ToggleButton marriged; //声明单选按钮 private RadioButton male,female; //声明文本编辑框 private EditText username,password; //声明下拉列表 private Spinner position; //声明多选按钮 private CheckBox reading,swimming; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置Activity页面布局 setContentView(R.layout.activity_main); //通过findViewById获得EditText对象 username = (EditText)findViewById(R.id.username); password = (EditText)findViewById(R.id.password); //通过findViewById方法获得RadioButton male = (RadioButton)findViewById(R.id.male); female =(RadioButton)findViewById(R.id.female); //通过findViewById获得ChechBox reading =(CheckBox)findViewById(R.id.reading); swimming =(CheckBox)findViewById(R.id.swimming); //通过findViewById获得ToggleButton实例 marriged =(ToggleButton)findViewById(R.id.marriged); //通过findViewById获得Spinner实例 position =(Spinner)findViewById(R.id.position); //下拉列表选项 String[] str = {"CEO","CFO","PM"}; //数组下拉列表适配器 ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item,str); //设置下拉列表适配器 position.setAdapter(aa); //通过findViewById方法获得Button的实例 register = (Button)findViewById(R.id.register); cancle =(Button)findViewById(R.id.cancle); //添加按钮的单击事件监听器 register.setOnClickListener(new OnClickListener(){ //点击事件方法 @Override public void onClick(View v) { // TODO Auto-generated method stub Bundle b= new Bundle(); //在Bundle中添加用户名称和用户密码 b.putString("username", "用户名称:"+username.getText().toString()); b.putString("password", "用户密码:"+password.getText().toString()); //在Bundle中添加性别 if(male.isChecked()){ b.putString("gender","性别:男"); }else{ b.putString("gender","性别:女"); } String temp= "爱好:"; if(reading.isChecked()){ temp+=" "; temp+="阅读"; } if(swimming.isChecked()) { temp+=" "; temp+="游泳"; } //在Bundle中添加爱好 b.putString("hobby",temp); //在Bundle中添加婚否 if(marriged.isChecked()){ b.putString("marriged","婚否:已婚"); }else{ b.putString("marriged","婚否:未婚"); } //在Bundle添加职位 b.putString("position","职位:"+position.getSelectedItem().toString()); //实例化Intent,跳转到ResultActivity Intent intent = new Intent(MainActivity.this,ResultActivity.class); //讲Bundle添加到Intent intent.putExtra("data", b); //启动Activity startActivity(intent); } }); } }
分析:1.Spinner制作下拉列表
步骤:①获取Spinner的实例
②产生下拉列表数组
③数组适配器
④设置下拉列表适配器
代码:
//通过findViewById获得Spinner实例 position =(Spinner)findViewById(R.id.position); //下拉列表选项 String[] str = {"CEO","CFO","PM"}; //数组下拉列表适配器 ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item,str); //设置下拉列表适配器 position.setAdapter(aa);
分析:2.Bundle用于从一个Activity传值到另一个Activity(其实有很多人说,用Intent的putExtra()方法就可以,但是,Bundle的优点在于传递复杂对象的值)
步骤:①实例化Bundle对象,保存属性
②调用Bundle的putString(key,value)方法,把属性保存到key里面
③实例化Intent,这是Android传递值得信使,没有它不行
④调用Intent的putExtra(key,value),这儿value换成Bundle的实例
⑤启动该Activity。调用方法startActivity(③中实例化Intent的对象)
代码:
Bundle b= new Bundle(); //在Bundle中添加用户名称和用户密码 b.putString("username", "用户名称:"+username.getText().toString()); b.putString("password", "用户密码:"+password.getText().toString()); //在Bundle中添加性别 if(male.isChecked()){ b.putString("gender","性别:男"); }else{ b.putString("gender","性别:女"); } String temp= "爱好:"; if(reading.isChecked()){ temp+=" "; temp+="阅读"; } if(swimming.isChecked()) { temp+=" "; temp+="游泳"; } //在Bundle中添加爱好 b.putString("hobby",temp); //在Bundle中添加婚否 if(marriged.isChecked()){ b.putString("marriged","婚否:已婚"); }else{ b.putString("marriged","婚否:未婚"); } //在Bundle添加职位 b.putString("position","职位:"+position.getSelectedItem().toString()); //实例化Intent,跳转到ResultActivity Intent intent = new Intent(MainActivity.this,ResultActivity.class); //讲Bundle添加到Intent intent.putExtra("data", b); //启动Activity startActivity(intent);
跳转到的页面:ResultActivity.java
package com.zhoujunwen.widget; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class ResultActivity extends Activity{ //声明ListView private ListView listView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置当前Activity界面布局 setContentView(R.layout.result); //通过findViewById方法获得ListView对象 listView=(ListView)findViewById(R.id.ListView01); //获得Intent Intent intent = this.getIntent(); //从Intent中获得Bundle Bundle b = intent.getBundleExtra("data"); //实例化List List list = new ArrayList(); //从Bundle中获得属性,添加到List list.add(b.getString("username")); list.add(b.getString("password")); list.add(b.getString("position")); list.add(b.getString("gender")); list.add(b.getString("hobby")); list.add(b.getString("marriged")); //实例化数组适配器 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_checked,list); //为ListView设置适配器 listView.setAdapter(adapter); } }
需要分析的地方:Intent获取传递到的值
//获得Intent Intent intent = this.getIntent(); //从Intent中获得Bundle Bundle b = intent.getBundleExtra("data"); //实例化List List list = new ArrayList(); //从Bundle中获得属性,添加到List list.add(b.getString("username")); list.add(b.getString("password")); list.add(b.getString("position")); list.add(b.getString("gender")); list.add(b.getString("hobby")); list.add(b.getString("marriged")); //实例化数组适配器 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_checked,list); //为ListView设置适配器 listView.setAdapter(adapter);
下面是两个布局文件:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TableLayout android:id="@+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:collapseColumns="3" android:stretchColumns="1"> <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="用户名称" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="" android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="用户密码" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="" android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="性别" android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <RadioGroup android:id="@+id/gender_g" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:text="男" android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:text="女" android:id="@+id/female" android:layout_width="wrap_content" android:layout_height="wrap_content"/>" </RadioGroup> </TableRow> <TableRow android:id="@+id/TableRow04" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="婚否" android:id="@+id/TextView04" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ToggleButton android:text="@+id/ToggleButton01" android:id="@+id/marriged" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android:id="@+id/TableRow05" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="爱好" android:id="@+id/hobby" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ChechBox android:text="阅读" android:id="@+id/reading" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ChechBox android:text="游泳" android:id="@+id/swimming" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android:id="@+id/TableRow06" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="职务" android:id="@+id/TextView05" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Spinner android:id="@+id/position" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android:id="@+id/TableRow05" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="取消" android:id="@+id/cancle" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:text="注册" android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> </TableLayout> </LinearLayout>
ResultActivity.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" > <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"/>" </LinearLayout>
时间: 2024-10-04 13:52:37