2015年7月14日 星期二

Android RadioGroup

成果展示:










































Project






程式碼:
mainwindow.xml
<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=".MainwindowActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/lblSelect"
            android:id="@+id/lblSelect" />

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/radiogroup"
            android:gravity="center">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rdoTemperature"
                android:id="@+id/radioButton1"
                android:checked="false" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rdoPosition"
                android:id="@+id/radioButton2"
                android:checked="false" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/rdoLinear"
                android:id="@+id/radioButton3"
                android:checked="false" />
        </RadioGroup>

    </LinearLayout>

</RelativeLayout>
MainwindowActivity
package com.test.mytransform;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class MainwindowActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainwindow);
        RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
        radiogroup.setOnCheckedChangeListener(listener);
    }

    private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId)
            {
                case (R.id.radioButton1):
                    Intent intent1 = new Intent();
                    intent1.setClass(MainwindowActivity.this, Window1Activity.class);
                    startActivity(intent1);
                    MainwindowActivity.this.finish();
                    break;
                case (R.id.radioButton2):
                    Intent intent2 = new Intent();
                    intent2.setClass(MainwindowActivity.this, Window2Activity.class);
                    startActivity(intent2);
                    MainwindowActivity.this.finish();
                    break;
                case (R.id.radioButton3):
                    Intent intent3 = new Intent();
                    intent3.setClass(MainwindowActivity.this, Window3Activity.class);
                    startActivity(intent3);
                    MainwindowActivity.this.finish();
                    break;
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_mainwindow, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

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

        return super.onOptionsItemSelected(item);
    }
}

沒有留言:

張貼留言