zoukankan      html  css  js  c++  java
  • Android Checkbox Example


    1. Custom String

    打开 “res/values/strings.xml” 文件,
    File : res/values/strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">CheckBoxDemo</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
        <string name="chk_ios">IPhone</string>
        <string name="chk_android">Android</string>
        <string name="chk_windows">Windows Mobile</string>
        <string name="btn_display">Display</string>
    </resources>
    






    2. CheckBox
    打开 “res/layout/activity_main.xml” 文件, 
    File : res/layout/activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
     
        <CheckBox
            android:id="@+id/chkIos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chk_ios" />
     
        <CheckBox
            android:id="@+id/chkAndroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chk_android"
            android:checked="true" />
     
        <CheckBox
            android:id="@+id/chkWindows"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chk_windows" />
     
        <Button
            android:id="@+id/btnDisplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_display" />
     
    </LinearLayout>

    注: android:checked="true"   表示默认选中。本例子中,chkAndroid默认选中




    3. Code Code

    package com.jiangge.checkboxdemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    	private CheckBox chkIos, chkAndroid, chkWindows;
    	private Button btnDisplay;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		addListenerOnChkIos();
    		addListenerOnButton();
    	}
    
    	private void addListenerOnChkIos() {
    		chkIos = (CheckBox) findViewById(R.id.chkIos);
    		
    		chkIos.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// chkIos 是否被选中
    				if (((CheckBox)v).isChecked()) {
    					Toast.makeText(getApplicationContext(), "Bro try android,哈哈",Toast.LENGTH_LONG).show();
    				}
    			}
    		});
    	}
    
    	private void addListenerOnButton() {
    		chkIos = (CheckBox) findViewById(R.id.chkIos);
    		chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
    		chkWindows = (CheckBox) findViewById(R.id.chkWindows);
    		btnDisplay = (Button) findViewById(R.id.btnDisplay);
    		
    		btnDisplay.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				StringBuffer result = new StringBuffer();
    				result.append("IPhone check : ").append(chkIos.isChecked());
    				result.append("
    Android check : ").append(chkAndroid.isChecked());
    				result.append("
    Windows Mobile check :").append(chkWindows.isChecked());
    		 
    				Toast.makeText(MainActivity.this, result.toString(),
    						Toast.LENGTH_LONG).show();				
    			}
    		});
    		
    	}
    
    
    }
    


    注:

    if (((CheckBox)v).isChecked())  向下转型。


    StringBuffer result = new StringBuffer();

    result .append().append() 


    4、运行结果:

    一启动时的运行结果:




    选中 IPhone (和Android)的运行结果:




    选中IPone 和  Winows Mobile,然后点击 Button 按钮的运行结果:


  • 相关阅读:
    java String 转Json报错 java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
    Idea 进行断点调试的 快捷键
    spring AOP的使用步骤
    AOP的定义和原理
    SpringBoot入门篇--使用IDEA创建一个SpringBoot项目
    详解CI、CD相关概念
    intellij idea 的全局搜索快捷键方法
    面向对象之工厂模式与构造函数模式的区别
    冒泡排序和快速排序
    java里的数组和list分别在什么情况下使用?
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3285789.html
Copyright © 2011-2022 走看看