08 January 2017

Android: Checkbox, RadioButton

activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <RadioGroup
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbt_android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Android" />

        <RadioButton
            android:id="@+id/rbt_ios"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="IOS" />
    </RadioGroup>

    <LinearLayout
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/cb_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Version 1" />

        <CheckBox
            android:id="@+id/cb_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Version 2" />

    </LinearLayout>

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"
        android:text="OK" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="25sp"
        android:layout_gravity="center"
        />
</LinearLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.demolisttenner;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView tvResult;

    private RadioButton rbtAndroid;
    private RadioButton rbtIOS;

    private CheckBox cbA;
    private CheckBox cbB;

    private Button btnOk;

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

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sologan = "";
                if (rbtAndroid.isChecked()) {
                    sologan = sologan + rbtAndroid.getText()+" - ";
                } else {
                    sologan = sologan + rbtIOS.getText()+" - ";
                }

                if (cbA.isChecked()) {
                    sologan = sologan + cbA.getText();
                }
                if (cbB.isChecked()) {
                    sologan = sologan + cbB.getText();
                }
                tvResult.setText(sologan);
            }
        });
    }

    public void initWidget() {
        tvResult = (TextView) findViewById(R.id.tv_result);

        rbtAndroid = (RadioButton) findViewById(R.id.rbt_android);
        rbtIOS = (RadioButton) findViewById(R.id.rbt_ios);

        cbA = (CheckBox) findViewById(R.id.cb_a);
        cbB = (CheckBox) findViewById(R.id.cb_b);

        btnOk = (Button) findViewById(R.id.btn_ok);
    }

}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang