Android开发之路八----UI组件

RadioGroup:这个类用于创建一组按钮之间相互排斥的单选按钮组,在同一个单选按钮组中勾选一个按钮则会取消改组中其它已经勾选的状态。

以上图为例,其xml代码如下:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <ScrollView 

         android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        

        ></ScrollView>

    <LinearLayout 

         android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical" 

        >

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="RadioDemo" />

    <RadioGroup 

        android:id="@+id/sexRg"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:checkedButton="@+id/woman"

        >

        <RadioButton 

            android:id="@id/woman"

            android:text="女"

            />

          <RadioButton 

            android:id="@+id/man"

            android:text="男"

            />

    </RadioGroup>

    <Button 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="addRadioButton"

        android:id="@+id/appendRadio"

        

        />

    </LinearLayout>

</LinearLayout>

其Java代码如下:

package cn.csdn.android.utext;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

public class Utest3Activity extends Activity implements OnCheckedChangeListener {

RadioGroup rg = null;

private static final String TAG = "TAG";

Button btn = null;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.radio);

findViews();

rg.check(R.id.man);// 设定某个选项被选中

// 获取当前选项组中被选中的选项的id

int checkedId = rg.getCheckedRadioButtonId();

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

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

}

private void findViews() {

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

// 注册监听器

rg.setOnCheckedChangeListener(this);

btn = (Button) this.findViewById(R.id.appendRadio);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 创建radioButton对象

RadioButton newRb = new RadioButton(Utest3Activity.this);

newRb.append("人妖");

newRb.setId(100);

// 添加到RadioGroup

rg.addView(newRb);

}

});

}

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

if (group.getId() == R.id.sexRg) {

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

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

}

}

}

CheckBox复选框是一种双状态按钮的特殊类型,可以选中或者不选中,如下图所示

其中xml代码如下:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="爱好"

        android:textSize="20dp" />

    <TableLayout

        android:id="@+id/tableLayout"

        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>

<Button 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:id="@+id/submit"

    />

</LinearLayout>

Java代码如下:

package cn.csdn.android.utext;

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

public class CheckBoxDemo extends Activity implements OnCheckedChangeListener{

private CheckBox cb1,cb2,cb3,cb4;

private Button btn;

private ArrayList<CheckBox> list=new ArrayList<CheckBox>();

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.checkbox);

findViews();

}

private void findViews() {

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);

}

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

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String fav="";

for(CheckBox cb:list){

fav+=cb.getText()+" , ";

}

Log.i("TAG", fav);

}

});

}

//覆盖CompoundButton.OnCheckChangeListener接口的抽象方法

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

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

}

}

ListView通过滚动条查看的视图列表,

1ListVeiw 用来展示列表的View

2.适配器 用来把数据映射到ListView上的中介。

3.数据    具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,

ArrayAdapter

SimpleAdapter

SimpleCursorAdapter

其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可  以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。

如下图所示:

xml代码如下:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <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" >

    </ListView>

</LinearLayout>

Java代码如下:

package cn.csdn.android.utext;

import android.app.ListActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class ListActivityDemo extends ListActivity {

String[] names = { "张三", "李四", "王五", "老李", "八戒" };

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

ArrayAdapter adapter = new ArrayAdapter(this,

android.R.layout.simple_list_item_1, names);

this.setListAdapter(adapter);

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

// TODO Auto-generated method stub

super.onListItemClick(l, v, position, id);

Log.i("TAG", names[position] + "positon = " + String.valueOf(position)

+ "row_id =" + String.valueOf(id));

}

}

Spinner

下拉列表(Spinner)是一个每次只能选择所有项中一项的部件。它的项来自于与之相关联的适配器中。

Xml代码如下:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="请选择一项运动项目" />

    <Spinner

        android:id="@+id/sportsSp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:entries="@array/sports"

        android:prompt="@string/spinner" />

</LinearLayout>

Java代码如下:

package cn.csdn.android.utext;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.TextView;

import android.widget.Spinner;

public class SpinnerDemo extends Activity implements OnItemSelectedListener {

Spinner sportSp = null;

private static final String TAG = "TAG";

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.spinner);

findViews();

}

private void findViews() {

sportSp = (Spinner) this.findViewById(R.id.sportsSp);

sportSp.setOnItemSelectedListener(this);

}

@Override

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

TextView tv = (TextView) arg1;

Log.i(TAG, tv.getText().toString());

}

@Override

public void onNothingSelected(AdapterView<?> arg0) {

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值