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

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章