第1课第1节_编写第1个Android应用程序实现按钮和复选框

 
 
 
D:\StudyAndroid\APP_0001_LEDDemo\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.harvey.app_0001_leddemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="XIANG TAN DA XUE CHEN HAI PAN!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/BUTTON"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ALL_ON"
        />

    <CheckBox
        android:id="@+id/LED1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED1"
        android:onClick="onCheckboxClicked"
        />

    <CheckBox
        android:id="@+id/LED2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED2"
        android:onClick="onCheckboxClicked"
        />

    <CheckBox
        android:id="@+id/LED3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED3"
        android:onClick="onCheckboxClicked"
        />

    <CheckBox
        android:id="@+id/LED4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LED4"
        android:onClick="onCheckboxClicked"
        />

</LinearLayout>

D:\StudyAndroid\APP_0001_LEDDemo\app\src\main\java\com\example\harvey\app_0001_leddemo\MainActivity.java

https://developer.android.com/guide/topics/ui/controls/checkbox.html

参考代码:

public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.checkbox_meat:
            if (checked)
                // Put some meat on the sandwich
            else
                // Remove the meat
            break;
        case R.id.checkbox_cheese:
            if (checked)
                // Cheese me
            else
                // I'm lactose intolerant
            break;
        // TODO: Veggie sandwich
    }
}

 

代码:

package com.example.harvey.app_0001_leddemo;

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

public class MainActivity extends AppCompatActivity {

    private Button button = null;
    private  boolean ledon = false;

    private CheckBox checkBoxLed1 = null;
    private CheckBox checkBoxLed2 = null;
    private CheckBox checkBoxLed3 = null;
    private CheckBox checkBoxLed4 = null;

    class MyButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            ledon = !ledon;
                if(ledon) {
                    button.setText("ALL OFF");
                    checkBoxLed1.setChecked(true);
                    checkBoxLed2.setChecked(true);
                    checkBoxLed3.setChecked(true);
                    checkBoxLed4.setChecked(true);
                }
                else {
                    button.setText("ALL ON");

                    checkBoxLed1.setChecked(false);
                    checkBoxLed2.setChecked(false);
                    checkBoxLed3.setChecked(false);
                    checkBoxLed4.setChecked(false);
                }
        }
    }
    public void onCheckboxClicked(View view) {
        // Is the view now checked?
        boolean checked = ((CheckBox) view).isChecked();

        // Check which checkbox was clicked
        switch(view.getId()) {
            case R.id.LED1:
                if (checked)
                {
                    Toast.makeText(getApplicationContext(),"LED1 on",Toast.LENGTH_SHORT).show();
                }
                // Put some meat on the sandwich
            else{
                    Toast.makeText(getApplicationContext(),"LED1 off",Toast.LENGTH_SHORT).show();
                }
                // Remove the meat
                break;
            case R.id.LED2:
                if (checked){
                    Toast.makeText(getApplicationContext(),"LED2 on",Toast.LENGTH_SHORT).show();
                }
                // Cheese me
            else{
                    Toast.makeText(getApplicationContext(),"LED2 off",Toast.LENGTH_SHORT).show();
                }
                // I'm lactose intolerant
                break;

            case R.id.LED3:
                if (checked){
                    Toast.makeText(getApplicationContext(),"LED3 on",Toast.LENGTH_SHORT).show();
                }
                // Cheese me
            else{
                    Toast.makeText(getApplicationContext(),"LED3 off",Toast.LENGTH_SHORT).show();
                }
                // I'm lactose intolerant
                break;

            case R.id.LED4:
                if (checked){
                    Toast.makeText(getApplicationContext(),"LED4 on",Toast.LENGTH_SHORT).show();
                }
                // Cheese me
            else{
                    Toast.makeText(getApplicationContext(),"LED4 off",Toast.LENGTH_SHORT).show();
                }
                // I'm lactose intolerant
                break;
            // TODO: Veggie sandwich
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.BUTTON);
        checkBoxLed1 = (CheckBox) findViewById(R.id.LED1);
        checkBoxLed2 = (CheckBox) findViewById(R.id.LED2);
        checkBoxLed3 = (CheckBox) findViewById(R.id.LED3);
        checkBoxLed4 = (CheckBox) findViewById(R.id.LED4);

        button.setOnClickListener(new MyButtonListener());
        //
//        button.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                // Perform action on click
//                ledon = !ledon;
//                if(ledon)
//                      button.setText("ALL OFF");
//                else
//                    button.setText("ALL ON");
//            }
//        });

    }
}

 


 

 
 
 

posted on 2017-06-08 15:24  竹林海宝  阅读(193)  评论(0编辑  收藏  举报

导航