標籤:android style blog http color os
1.布局
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性別:"/> <RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_gravity="center_horizontal" > <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <RadioButton android:id="@+id/female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup></TableRow><TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜歡的顏色"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="紅色" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="藍色" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="綠色" /> </LinearLayout></TableRow><TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content"/></TableLayout>
首先整體用的是TableLayout,表格版面配置
然後將單選框那部分放在TableRow裡,裡面有一個TextView和兩個RadioButton(這兩個用RadioGroup括起來,形成一個整體)
還有多選框部分,用TableRow括起來,裡面有三個checkbox用LinearLayout括起來。形成線性布局,豎直方向的。
最後的是單獨放了一個TextView顯示結果等。
2.實現選擇單選框顯示一句話。
package com.example.checkbutton;import android.app.Activity;import android.os.Bundle;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class MainActivity extends Activity {RadioGroup rg;TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg=(RadioGroup) findViewById(R.id.rg); show=(TextView) findViewById(R.id.show); rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub String tip=checkedId==R.id.male? "您的性別是男人":"您的性別是女人"; show.setText(tip); } }); }}
將兩個RadioBox的整體RadioGroup作為一個對象,對他進行事件監聽。
用了條件運算子,String tip=checkedId==R.id.male? "您的性別是男人":"您的性別是女人";
最後setText就成。