Android学习-组件2
来源:互联网 发布:php视频文件上传 编辑:程序博客网 时间:2024/05/30 23:45
一、列表组件:
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 implementsOnCheckedChangeListener {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);// 获取当前选项组中被选中的选项的IDint 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() {@Overridepublic 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 Activityimplements 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());
}
}
实现效果:
- Android学习-组件2
- Android 自定义组件学习 2
- Android学习笔记2---组件
- Android学习笔记2:Android四大组件
- Android学习第2步--Activity组件
- 学习笔记<2>Android基本四大组件
- 【笨鸟先飞】android重新学习日记2---组件
- Android基本组件学习
- 学习android listview组件
- Android学习--Activity组件
- Android四大组件学习
- Android组件Service学习
- Android Fragment组件学习
- Android组件ViewPagerIndicator学习
- Android组件Volley学习
- Android 组件学习--CheckBox
- Android Fragment 组件学习
- Android 组件学习笔记
- Android下的任务和Activity栈
- Android的一些开源项目集锦 以备以后研究
- 结构型模式
- 免费的天气预报webservice接口 .保留
- Cookie学习之一
- Android学习-组件2
- 创建型模式
- 如何成为一个C++高级程序员
- Android 签名详解
- 怎样才能开发出好的软件
- 给年轻程序员的几句话
- 提炼方法 Extract Method
- Android进阶:Google自带语音播放功能实现
- python在自动化测试中的使用