RadioButton練習(android)

來源:互聯網
上載者:User

Radio 單選是由 RadioGroup 與 RadioButton 組成的,這個很簡單,看代碼就會了

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你的性別:請選擇答案 " />
    <RadioGroup 
        android:id="@+id/rg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_x = "3px"
        android:layout_y = "54px"
        >
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男生"
             />
         <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女生"
             />
    </RadioGroup>
    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="測試"
        />
</LinearLayout>

 

代碼

 

package zziss.android.radiotest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioTestActivity extends Activity {
    /** Called when the activity is first created. */
    RadioGroup rg1 ;
    RadioButton rb1;
    RadioButton rb2;
    Button      ibtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rg1 = (RadioGroup)this.findViewById(R.id.rg1);
        rb1 = (RadioButton)this.findViewById(R.id.rb1);
        rb2 = (RadioButton)this.findViewById(R.id.rb2);
        
        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if (checkedId == rb1.getId())
                {
                    ShowInfo(rb1.getText().toString());
                }
                if (checkedId == rb2.getId())
                {
                    ShowInfo(rb2.getText().toString());
                }
            }
        });
        
        ibtn = (Button)this.findViewById(R.id.btn);
        ibtn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int id = rg1.getCheckedRadioButtonId();
                if (id != -1)
                    showIdText(id);
                else
                    ShowInfo("請選擇");
            }
        });
    }
    
    private void ShowInfo(String str)
    {
        Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        toast.show();
    }
    private void showIdText(int aId)
    {
        ShowInfo(((RadioButton)this.findViewById(aId)).getText().toString());
    }
    
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.