android基础页面跳转
2011-12-14 21:32
211 查看
今天主要讲了单选框(RadioButton,RiaioGroup(分组))、多选框(CheckBox)、列表显示(ListView)、下拉列列表框(Spinner)。
要完成单选框的显示,我们需要使用到RadioGropup和RadioButton(单选框)RadioGroup对单选框进行分组,相同组内的单选框只有一个单选框被选中。
常用的方法有:
RadioGroup.check(int id);将指定的RadioButton设置为选中状态。
(RadioButton)findViewById(RadioGroup.getCheckedRadioButton());获取被选中的单选框
RadioButton.getText();获取单选框的值。
调用setOnCheckedChangeListener()方法,处理单选框备选事件。
实例:
<TableRow
android:gravity="center">
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="20dp"/>
<RadioGroup
android:id="@+id/sexRg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/woman">"
<RadioButton
android:id="@id/woman"
android:text="女"/>
<RadioButton
android:id="@+id/man"
android:text="男"/>"
</RadioGroup>"
</TableRow>
多选框(CheckBox)
CheckBox.getChecked(true);将CheckBox设置成选中状态。
CheckBox.getText();获取多选框的值。
CheckBox.isCheckBox();判断该选项是否被选中。
调用
setOnCheckedChangeListener ()
方法 , 处 理 多 选 框 被 选 择 事 件 , 把
CompoundButton.OnCheckedChangeListener
实例作为参数传入
<TableRow
android:gravity="center">
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提示"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="私人信息"
/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="公共信息"/>
</LinearLayout>
"
</TableRow>
<TableRow
android:gravity="center">
<TextView
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="系统信息"
/>
<CheckBox
android:id="@+id/box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="好友信息"/>
</LinearLayout>
</TableRow>
ListView显示列表:
</TableRow>
setAdapter(ListAdapter adapter)
为
ListView
绑定一个
Adapter
setChoiceMode(int choiceMode)
为
ListView
指定一个显示的模式,可选值有三个
CHOICE_MODE_NONE
(默认值,没
有单选或多选效果) 、
CHOICE_MODE_SINGLE
(单选框效果 ) 、
CHOICE_MODE_MULTIPLE
(多选框效果) ;
setOnItemClickListener (AdapterView.OnItemClickListener listener)
为其注册一个元素被点击事件的监听器,当其中某一项被点击时调用其参数
listener
中的
onItemClick
()方法。
1.4.4
下拉列表框
(Spinner)
手机的屏幕较小,因此使用下拉列表来进行选择式输入是一个非常好的方式。
Spinne r
与
ListView
一样,也是
AdapterView
的一个间接子类,是一个显示数据的窗口。
Spinner
类常用的方法如下:
Spinner.getItemAtPosition(Spinner.getSelectedItemPosition());
获取下拉列
表框的值
调用
setOnItemSelectedListener ()
方法,处理下拉列表框被选择事件,把
AdapterView.OnItemSelectedListener
实例作为参数传入.
<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<string-array
name="citys">
<item>河北省</item>
<item>山东省</item>
<item>海南省</item>
<item>辽宁省</item>
<item>贵州省</item>
</string-array>
</resources>
<TableRow
android:gravity="center">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择省份"
android:textSize="20dp"/>
<Spinner
android:id="@+id/citySp"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:prompt="@string/city_name"
android:entries="@array/citys"/>
</TableRow>
实例:从一个界面跳到另一个界面并列表显示。
package cn.class3g;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
public class UITextActivity extends Activity implements OnItemSelectedListener,OnCheckedChangeListener,android.widget.RadioGroup.OnCheckedChangeListener{
Spinner citySp=null;
CheckBox box1,box2,box3,box4;
RadioGroup rg=null;
Button btn=null;
RadioButton rb;
private ArrayList<CheckBox> list=new ArrayList<CheckBox>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
findViews();
}
private void findViews(){
citySp=(Spinner) this.findViewById(R.id.citySp);
citySp.setOnItemSelectedListener(this);
citySp.performClick();
box1=(CheckBox) this.findViewById(R.id.box1);
box2=(CheckBox) this.findViewById(R.id.box2);
box3=(CheckBox) this.findViewById(R.id.box3);
box4=(CheckBox) this.findViewById(R.id.box4);
list.add(box1);
list.add(box2);
list.add(box3);
list.add(box4);
for(CheckBox cb:list){
//因为UITextActivity
实现了监听器的接口,所以UITextActivity可以当做监听对象
//作为参数放在setOnCheckedChangeListener()方法中作参数。
cb.setOnCheckedChangeListener(this);
}
rg=(RadioGroup) this.findViewById(R.id.sexRg);
//获取当前选项组中选中选项的id.
int checkedId=rg.getCheckedRadioButtonId();
rb=(RadioButton) this.findViewById(checkedId);
//注册监听器
rg.setOnCheckedChangeListener(this);
btn=(Button) this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Bundle b=new Bundle();
//获取复选框的内容
String fav="";
for(CheckBox cb:list){
if(cb.isChecked()){
fav+=cb.getText()+" ";
}
}
//获取单选按钮的字符
String str=rb.getText().toString();
//获取所选省份
String city=citySp.getSelectedItem().toString();
b.putString("citySp", "省份:"+city);
b.putString("rb", "性别:"+str);
b.putString("cb", "爱好:"+fav);
Intent intent=new Intent(UITextActivity.this,ListsActivity.class);
intent.putExtra("string", b);
startActivity(intent);
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv=(TextView) arg1;
Log.i("lifeCycle",tv.getText().toString() );
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("lifeCycle", buttonView.getText().toString());
}
// 覆盖OnCheckedChangeListener接口的抽象方法
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(group.getId()==R.id.sexRg){
//获取当前选项组中选中选项的id.
RadioButton rb=(RadioButton) this.findViewById(checkedId);
Log.i("lifeCycle", rb.getText().toString());
}
}
}
package cn.class3g;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=this.getIntent();
Bundle b=intent.getBundleExtra("string");
List<String> list=new ArrayList<String>();
list.add(b.getString("citySp"));
list.add(b.getString("rb"));
list.add(b.getString("cb"));
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list);
this.setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
}
要完成单选框的显示,我们需要使用到RadioGropup和RadioButton(单选框)RadioGroup对单选框进行分组,相同组内的单选框只有一个单选框被选中。
常用的方法有:
RadioGroup.check(int id);将指定的RadioButton设置为选中状态。
(RadioButton)findViewById(RadioGroup.getCheckedRadioButton());获取被选中的单选框
RadioButton.getText();获取单选框的值。
调用setOnCheckedChangeListener()方法,处理单选框备选事件。
实例:
<TableRow
android:gravity="center">
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="20dp"/>
<RadioGroup
android:id="@+id/sexRg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/woman">"
<RadioButton
android:id="@id/woman"
android:text="女"/>
<RadioButton
android:id="@+id/man"
android:text="男"/>"
</RadioGroup>"
</TableRow>
多选框(CheckBox)
CheckBox.getChecked(true);将CheckBox设置成选中状态。
CheckBox.getText();获取多选框的值。
CheckBox.isCheckBox();判断该选项是否被选中。
调用
setOnCheckedChangeListener ()
方法 , 处 理 多 选 框 被 选 择 事 件 , 把
CompoundButton.OnCheckedChangeListener
实例作为参数传入
<TableRow
android:gravity="center">
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提示"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="私人信息"
/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="公共信息"/>
</LinearLayout>
"
</TableRow>
<TableRow
android:gravity="center">
<TextView
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="系统信息"
/>
<CheckBox
android:id="@+id/box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="好友信息"/>
</LinearLayout>
</TableRow>
ListView显示列表:
</TableRow>
setAdapter(ListAdapter adapter)
为
ListView
绑定一个
Adapter
setChoiceMode(int choiceMode)
为
ListView
指定一个显示的模式,可选值有三个
CHOICE_MODE_NONE
(默认值,没
有单选或多选效果) 、
CHOICE_MODE_SINGLE
(单选框效果 ) 、
CHOICE_MODE_MULTIPLE
(多选框效果) ;
setOnItemClickListener (AdapterView.OnItemClickListener listener)
为其注册一个元素被点击事件的监听器,当其中某一项被点击时调用其参数
listener
中的
onItemClick
()方法。
1.4.4
下拉列表框
(Spinner)
手机的屏幕较小,因此使用下拉列表来进行选择式输入是一个非常好的方式。
Spinne r
与
ListView
一样,也是
AdapterView
的一个间接子类,是一个显示数据的窗口。
Spinner
类常用的方法如下:
Spinner.getItemAtPosition(Spinner.getSelectedItemPosition());
获取下拉列
表框的值
调用
setOnItemSelectedListener ()
方法,处理下拉列表框被选择事件,把
AdapterView.OnItemSelectedListener
实例作为参数传入.
<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<string-array
name="citys">
<item>河北省</item>
<item>山东省</item>
<item>海南省</item>
<item>辽宁省</item>
<item>贵州省</item>
</string-array>
</resources>
<TableRow
android:gravity="center">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择省份"
android:textSize="20dp"/>
<Spinner
android:id="@+id/citySp"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:prompt="@string/city_name"
android:entries="@array/citys"/>
</TableRow>
实例:从一个界面跳到另一个界面并列表显示。
package cn.class3g;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
public class UITextActivity extends Activity implements OnItemSelectedListener,OnCheckedChangeListener,android.widget.RadioGroup.OnCheckedChangeListener{
Spinner citySp=null;
CheckBox box1,box2,box3,box4;
RadioGroup rg=null;
Button btn=null;
RadioButton rb;
private ArrayList<CheckBox> list=new ArrayList<CheckBox>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
findViews();
}
private void findViews(){
citySp=(Spinner) this.findViewById(R.id.citySp);
citySp.setOnItemSelectedListener(this);
citySp.performClick();
box1=(CheckBox) this.findViewById(R.id.box1);
box2=(CheckBox) this.findViewById(R.id.box2);
box3=(CheckBox) this.findViewById(R.id.box3);
box4=(CheckBox) this.findViewById(R.id.box4);
list.add(box1);
list.add(box2);
list.add(box3);
list.add(box4);
for(CheckBox cb:list){
//因为UITextActivity
实现了监听器的接口,所以UITextActivity可以当做监听对象
//作为参数放在setOnCheckedChangeListener()方法中作参数。
cb.setOnCheckedChangeListener(this);
}
rg=(RadioGroup) this.findViewById(R.id.sexRg);
//获取当前选项组中选中选项的id.
int checkedId=rg.getCheckedRadioButtonId();
rb=(RadioButton) this.findViewById(checkedId);
//注册监听器
rg.setOnCheckedChangeListener(this);
btn=(Button) this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Bundle b=new Bundle();
//获取复选框的内容
String fav="";
for(CheckBox cb:list){
if(cb.isChecked()){
fav+=cb.getText()+" ";
}
}
//获取单选按钮的字符
String str=rb.getText().toString();
//获取所选省份
String city=citySp.getSelectedItem().toString();
b.putString("citySp", "省份:"+city);
b.putString("rb", "性别:"+str);
b.putString("cb", "爱好:"+fav);
Intent intent=new Intent(UITextActivity.this,ListsActivity.class);
intent.putExtra("string", b);
startActivity(intent);
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv=(TextView) arg1;
Log.i("lifeCycle",tv.getText().toString() );
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("lifeCycle", buttonView.getText().toString());
}
// 覆盖OnCheckedChangeListener接口的抽象方法
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(group.getId()==R.id.sexRg){
//获取当前选项组中选中选项的id.
RadioButton rb=(RadioButton) this.findViewById(checkedId);
Log.i("lifeCycle", rb.getText().toString());
}
}
}
package cn.class3g;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=this.getIntent();
Bundle b=intent.getBundleExtra("string");
List<String> list=new ArrayList<String>();
list.add(b.getString("citySp"));
list.add(b.getString("rb"));
list.add(b.getString("cb"));
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list);
this.setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
}
相关文章推荐
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- Android基础之初探activity(页面跳转,数据传输)
- android 页面跳转(基础知识)
- Android基础笔记(七)-页面跳转和数据传递
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- Android应用开发基础之六:页面跳转和数据传递
- android基础之页面间跳转传值,用Bundle+Intent
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- 【Android基础】页面跳转与传值(Activity跳转与传值) .
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- android基础页面跳转
- Android基础笔记(七)-页面跳转和数据传递
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- 【Android基础】页面跳转与传值(Activity跳转与传值)
- Android基础】页面跳转与传值(Activity跳转与传值)
- Android基础】页面跳转与传值(Activity跳转与传值)