Android组件RadioButton、checkBox、listview、spiner综合实例及Intent传值
组件解释:
RadioButton单选按钮
checkBox复选框是一种有双状态按钮的特殊类型,可以选中或者不选中。
listview列表
列表的显示需要三个元素:1.ListVeiw 用来展示列表的View。2.适配器 用来把数据映射到ListView上的中。3.数据 具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
spiner下拉列表(Spinner)是一个每次只能选择所有项中一项的部件。它的项来自于与之相关联的适配器中。
Intent英文里 Intent是“意向、打算”的意思,其实就是告诉别人你的意图的意思了,这么理解Android里面的Intent也就不难了。
费话不多说,直接先看效果图:
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:layout_gravity=”center”
- android:gravity=”center”
- android:text=”@string/hello”
- android:textSize=”30dp”
- android:textStyle=”bold” />
- <TableLayout
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:stretchColumns=”1″ >
- <TableRow >
- <TextView
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_gravity=”center”
- android:text=”用户名:” />
- <EditText
- android:id=”@+id/username”
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:hint=”请输入用户名!!!” />
- </TableRow>
- <TableRow >
- <TextView
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_gravity=”center”
- android:text=”密码:” />
- <EditText
- android:id=”@+id/password”
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:hint=”请输入密码!!!” />
- </TableRow>
- <TableRow >
- <TextView
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”
- android:layout_gravity=”center”
- android:text=”性别:”
- android:textSize=”20dp” />
- <RadioGroup
- android:id=”@+id/sex”
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:checkedButton=”@+id/woman”
- android:orientation=”horizontal” >
- <RadioButton
- android:id=”@+id/nan”
- android:text=”男” />
- <RadioButton
- android:id=”@id/woman”
- android:text=”女” />
- </RadioGroup>
- </TableRow>
- <TableRow >
- <TextView
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:layout_gravity=”center”
- android:text=”爱好:”
- android:textSize=”20dp” />
- <TableLayout
- android:id=”@+id/table”
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:stretchColumns=”*” >
- <TableRow >
- <CheckBox
- android:id=”@+id/cb1″
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:text=”足球” />
- <CheckBox
- android:id=”@+id/cb2″
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:text=”篮球” />
- </TableRow>
- <TableRow >
- <CheckBox
- android:id=”@+id/cb3″
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:text=”游戏” />
- <CheckBox
- android:id=”@+id/cb4″
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:text=”游泳” />
- </TableRow>
- </TableLayout>
- </TableRow>
- <TableRow >
- <TextView
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”
- android:text=”学历:” />
- <Spinner
- android:id=”@+id/sports”
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:entries=”@array/educate”
- android:prompt=”@string/educate” />
- </TableRow>
- </TableLayout>
- <Button
- android:layout_width=”100dp”
- android:layout_height=”wrap_content”
- android:layout_gravity=”center”
- android:id=”@+id/button”
- android:text=”注册” />
- </LinearLayout>
RegisterActivity.java实现代码:
- package cn.csdn.activity;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Spinner;
- import android.widget.TextView;
- public class RegisterActivity extends Activity implements OnClickListener{
- String[] values=new String[5];//存放所有的值
- Button button=null;
- EditText username=null;//用户名
- EditText password=null;//密码
- RadioGroup rg=null;
- private CheckBox cb1,cb2,cb3,cb4;
- Spinner educate=null;
- private ArrayList<CheckBox> list=new ArrayList<CheckBox>();
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViews();
- }
- private void findViews() {
- button=(Button) findViewById(R.id.button);
- button.setOnClickListener(this);
- username=(EditText) findViewById(R.id.username);//用户名
- password=(EditText) findViewById(R.id.password);//密码
- rg=(RadioGroup) findViewById(R.id.sex);//性别
- cb1=(CheckBox) findViewById(R.id.cb1);//爱好 复选框
- cb2=(CheckBox) findViewById(R.id.cb2);
- cb3=(CheckBox) findViewById(R.id.cb3);
- cb4=(CheckBox) findViewById(R.id.cb4);
- list.add(cb1);
- list.add(cb2);
- list.add(cb3);
- list.add(cb4);
- educate=(Spinner) findViewById(R.id.sports);//学历 下拉列表
- }
- @Override
- public void onClick(View v) {
- values[0]=username.getText().toString();
- values[1]=password.getText().toString();
- values[2]=((RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
- String fav=””;
- for(CheckBox cb:list){
- if(cb.isChecked()){
- fav+=cb.getText()+” “;
- }
- }
- values[3]=fav;
- values[4]=educate.getSelectedItem().toString();
- //往第二个activity传值
- Intent intent=new Intent();
- intent.setClass(RegisterActivity.this, OtherIntentActivity.class);
- //intent.setClass(第一个Activity.this, 要跳转的Activity.class);
- intent.putExtra(“values”, values);
- //intent.putExtra(“参数”, “参数值”);
- startActivity(intent);
- }
- }
第二个页面:OtherIntentActivity
- package cn.csdn.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ArrayAdapter;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- public class OtherIntentActivity extends Activity {
- ListView list=null;
- String value[]=new String[5];
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.other);
- //接值
- Intent intent = getIntent();
- value = intent.getStringArrayExtra(“values”);
- value[0]=”用户名:”+value[0];
- value[1]=”密码:”+value[1];
- value[2]=”性别:”+value[2];
- value[3]=”爱好:”+value[3];
- value[4]=”学历:”+value[4];
- list=(ListView) findViewById(R.id.list);
- ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,value);
- list.setAdapter(adapter);
- }
- }
下面说一下intent传值:
//传值
Intent _intent = new Intent(); _intent.setClass(第一个Activity.this, 要跳转的Activity.class); _intent.putExtra(“参数”, “参数值”);
this.startActivity(_intent);
//第二个页面接收
Intent _intent = getIntent(); String _value = _intent.getStringExtra(“参数”);