<resources>
    <string name="app_name">CheckToggle</string>
    <string name="action_settings">Settings</string>
    <string name="married">已婚</string>
    <string name="single">未婚</string>
    <string name="working">有工作</string>
    <string name="notworking">待業中</string>
    <string name="haskid">有小孩</string>
    <string name="nokid">沒小孩</string>
</resources>


<LinearLayout 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"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/married"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/married" />

    <ToggleButton
        android:id="@+id/job"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/working"
        android:textOff="@string/notworking" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
        android:id="@+id/kid"
        android:textOff="@string/nokid"
        android:textOn="@string/haskid" />

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""    />

</LinearLayout>



package com.lcpan.checktoggle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity implements OnClickListener {
    private CheckBox married;
    private ToggleButton job;
    private ToggleButton kid;
    private TextView output;
    private String msg1, msg2, msg3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        married = (CheckBox)findViewById(R.id.married);
        job = (ToggleButton)findViewById(R.id.job);
        kid = (ToggleButton)findViewById(R.id.kid);
        output = (TextView)findViewById(R.id.output);
        msg1 = getString(R.string.single);
        msg2 = ", " + getString(R.string.notworking);
        msg3= ", " + getString(R.string.nokid);
        output.setText(msg1 + msg2+msg3);
        married.setOnClickListener(this);
        job.setOnClickListener(this);
        kid.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch(v.getId()){
            case R.id.married:
                if (((CheckBox)v).isChecked())
                    msg1 = getString(R.string.married);
                else
                    msg1 = getString(R.string.single);
                break;
            case R.id.job:
                if (((ToggleButton)v).isChecked())
                    msg2 = ", " + getString(R.string.working);
                else
                    msg2 = ", " + getString(R.string.notworking);
                break;
            case R.id.kid:
                if (((ToggleButton)v).isChecked())
                    msg3 = ", " + getString(R.string.haskid);
                else
                    msg3 = ", " + getString(R.string.nokid);
                break;
        }
        output.setText(msg1 + msg2+msg3);
    }

    @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) 人氣()