<resources>
    <string name="app_name">RadioButton</string>
    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>
    <string name="yellow">Yellow</string>
    <string name="green">Green</string>
</resources>



<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/group1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="56dp"
        android:layout_marginTop="78dp"
        android:orientation="horizontal" >   垂直分布

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="@string/yellow"
            android:checked="true" />    預設值

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="@string/green" />
    </RadioGroup>

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/group1"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="22dp"
        android:text="@string/hello_world" />

</RelativeLayout>


package com.lcpan.radiobutton;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private TextView output;
    private RadioGroup group1;
    private RadioButton radio1, radio2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        output = (TextView)findViewById(R.id.output);
        group1 = (RadioGroup)findViewById(R.id.group1);
        radio1 = (RadioButton)findViewById(R.id.radio1);
        radio2 = (RadioButton)findViewById(R.id.radio2);
        group1.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(RadioGroup rg, int checkedid) {
        if (checkedid == radio1.getId()) //checkedid是被選取的ID
            output.setTextColor(Color.YELLOW);
        else if (checkedid == radio2.getId())
            output.setTextColor(Color.GREEN);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       // getMenuInflater().inflate(R.menu.menu_main, menu); 沒有用到
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       // int id = item.getItemId();

       // if (id == R.id.action_settings) {
        //    return true;
      //  }

        return super.onOptionsItemSelected(item);
    }
}
創作者介紹

學習筆記專區

乙方 發表在 痞客邦 PIXNET 留言(0) 人氣()