安卓學習第7課——checkbutton

來源:互聯網
上載者:User

標籤: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就成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.