您的位置:首页 > 移动开发 > Android开发

Android学习-组件2

2011-12-14 23:37 302 查看
一、列表组件:
ListView组件用于列表的的形式显示数据,ListView组件在装载数据时并不是直接使用ListView类的add方法或类的方法添加数据,而是需要指定一个Adapter对象,通过控制器获得显示数据,
1、 首先定义ListView组件xml文件如下:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="名单" />

<ListView
android:id="@+id/nameList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
2、创建一个Adapter对象然后添加适配器,具体java代码:



3、运行效果如图:



二、 下拉列表组件:Spinner
1、 Spinner组件用于显示一个下来列表,该组件用法与ListView组件类似,并在装载数据的时候指定要装载的数据,(数组或list对象)下面使用ArrayAdapter对象向组件添加数据,



2.所需要配置的XML文件,分别在values和layout文件



<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="sports">
<item>足球</item>
<item>篮球</item>
<item>网球</item>
<item>棒球</item>
</string-array>

</resources>

3.单选按钮和复选按钮:
1、单选按钮和复选按钮都是继承了Button是所有用户界面中最普通的UI组件,也因此他们都可以直接使用Button支持各种属性和方法。
RadioButton和CheckBox与普通按钮不同的是,他们多了一个可选中的功能,因此RadioButton,和Check多了一个额外的android:check功能,该属性用于指定选框是否被选中,

RadioButton只能选中一个,也就是互斥按钮,而CheckBox可以选多个,

下面是具体的实现代码:
动态添加的代码块
RadioButton rb = new RadioButton(UITestActivity.this);
rb.append("xxx");
rb.setId(111);
rg.addView(rb);
rg.check(R.id.man);


XML代码:
<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="fill_parent"
android:layout_height="wrap_content"
android:text="篮球" />

<CheckBox
android:id="@+id/cb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="足球" />
</TableRow>

<TableRow >

<CheckBox
android:id="@+id/cb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="游泳" />

<CheckBox
android:id="@+id/cb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下棋" />
</TableRow>
</TableLayout>

<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提交" />

/>
具体实现的java代码:

public class UITest2Activity extends Activity implements
OnCheckedChangeListener {

RadioGroup rg = null;
Button btn = null;

private static final String TAG = "TAG";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_layout);

findView();

// 指定选项
rg.check(R.id.woman);

// 获取当前选项组中被选中的选项的ID
int checkedId = rg.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) this.findViewById(checkedId);
Log.i(TAG, rb.getText().toString());
}

private void findView() {

rg = (RadioGroup) this.findViewById(R.id.sexRg);
btn = (Button) this.findViewById(R.id.button);

rg.setOnCheckedChangeListener(this);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

// 很关键的地方创建对象
// 匿名内部类。this指当前类实例,监听器实例*****加前缀的话就会转化到山下文实例
RadioButton newRb = new RadioButton(UITest2Activity.this);
newRb.append("妖");
newRb.setId(10);
// 添加到group中;
rg.addView(newRb);

}

});

}

// 覆盖接口的抽象方法
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if (arg0.getId() == R.id.sexRg) {

RadioButton rb = (RadioButton) this.findViewById(arg1);

Log.i(TAG, rb.getText().toString());
}

}

}




2.checkbox的具体实现代码:

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="fill_parent"
android:layout_height="wrap_content"
android:text="篮球" />

<CheckBox
android:id="@+id/cb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="足球" />
</TableRow>

<TableRow >

<CheckBox
android:id="@+id/cb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="游泳" />

<CheckBox
android:id="@+id/cb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下棋" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提交" />

/>

Java代码:
public class CheckBox1 extends Activity
implements OnCheckedChangeListener {

private CheckBox cb1, cb2, cb3, cb4;
private ArrayList<CheckBox> list = new ArrayList<CheckBox>();
private Button submitBtn;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);

findViews();
}

private void findViews() {

submitBtn = (Button) this.findViewById(R.id.submit);

cb1 = (CheckBox) this.findViewById(R.id.cb1);
cb2 = (CheckBox) this.findViewById(R.id.cb2);
cb3 = (CheckBox) this.findViewById(R.id.cb3);
cb4 = (CheckBox) this.findViewById(R.id.cb4);

list.add(cb1);
list.add(cb2);
list.add(cb3);
list.add(cb4);

for (CheckBox cb : list) {

//this 代表的是当前类实例的对象,需要的是监听器对像,
//这个当前实例正好实现了监听器接口所以this可以当做监听器对象,放入方法之中做参数
cb.setOnCheckedChangeListener(this);
}

submitBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String fav = " ";
for(CheckBox cb:list){
if(cb.isChecked()){
fav += cb.getText()+"__";

}

}
Log.i("TAG", fav);
}
});

}

public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {

Log.i("TAG", buttonView.getText().toString());

}

}
实现效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: