Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能示例
本文實例講述了Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能。分享給大家供大家參考,具體如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:checked="false"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
</LinearLayout>
MainActivity.java
package com.example.hello;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
checkBox = (CheckBox) findViewById(R.id.checkBox1);
//通過設(shè)置checkbox的監(jiān)聽事件,判斷checkbox是否被選中
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 通過這個方法,來監(jiān)聽當前的checkbox是否被選中
if (isChecked) {
//獲得checkBox的文本內(nèi)容
String text = checkBox.getText().toString();
Toast.makeText(MainActivity.this, "111", 1).show();
}
}
});
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android CheckBox中設(shè)置padding無效解決辦法
- Android 中CheckBox多項選擇當前的position信息提交的示例代碼
- Android 中CheckBox的isChecked的使用實例詳解
- Android開發(fā)手冊自定義Switch開關(guān)按鈕控件
- Android開關(guān)控件Switch的使用案例
- Android 自定義Switch開關(guān)按鈕的樣式實例詳解
- Android UI控件Switch的使用方法
- Android單選按鈕RadioButton的使用方法
- Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
相關(guān)文章
在android開發(fā)中進行數(shù)據(jù)存儲與訪問的多種方式介紹
很多時候我們的軟件需要對處理后的數(shù)據(jù)進行存儲或再次訪問,Android為數(shù)據(jù)存儲提供了多種方式,首先給大家介紹使用文件如何對數(shù)據(jù)進行存儲,感興趣的朋友可以了解下哈2013-06-06
解決Eclipse創(chuàng)建android項目無法正常預(yù)覽布局文件問題的方法
這篇文章主要介紹了解決Eclipse創(chuàng)建android項目無法正常預(yù)覽布局文件問題的方法,需要的朋友可以參考下2015-12-12
android仿微信通訊錄搜索示例(匹配拼音,字母,索引位置)
本篇文章主要介紹了android仿微信通訊錄搜索示例(匹配拼音,字母,索引位置),具有一定的參考價值,有興趣的可以了解一下2017-09-09

