Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能示例
本文實(shí)例講述了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)聽當(dāng)前的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ā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android CheckBox中設(shè)置padding無效解決辦法
- Android 中CheckBox多項(xiàng)選擇當(dāng)前的position信息提交的示例代碼
- Android 中CheckBox的isChecked的使用實(shí)例詳解
- Android開發(fā)手冊自定義Switch開關(guān)按鈕控件
- Android開關(guān)控件Switch的使用案例
- Android 自定義Switch開關(guān)按鈕的樣式實(shí)例詳解
- Android UI控件Switch的使用方法
- Android單選按鈕RadioButton的使用方法
- Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
相關(guān)文章
Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Android 簡單的彈出框(在屏幕中間,傳string[],根據(jù)內(nèi)容框框大小自適應(yīng))
這篇文章主要介紹了Android 簡單的彈出框(在屏幕中間,傳string[],根據(jù)內(nèi)容框框大小自適應(yīng)),需要的朋友可以參考下2017-04-04Android貝塞爾曲線實(shí)現(xiàn)消息拖拽消失
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實(shí)現(xiàn)消息拖拽消失,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Android PullToRefreshLayout下拉刷新控件的終結(jié)者
這篇文章主要介紹了Android自定義控件實(shí)戰(zhàn)中下拉刷新控件終結(jié)者PullToRefreshLayout的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03android開發(fā)教程之handle實(shí)現(xiàn)多線程和異步處理
這篇文章主要介紹了android的handle實(shí)現(xiàn)多線程和異步處理的示例,大家參考使用吧2014-01-01Android開發(fā)之ViewPager實(shí)現(xiàn)滑動切換頁面
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之ViewPager實(shí)現(xiàn)滑動切換頁面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09Android BroadcastReceiver傳輸機(jī)制詳解
Android開發(fā)的四大組件分別是:活動(activity),用于表現(xiàn)功能;服務(wù)(service),后臺運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個應(yīng)用中存儲和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件2023-01-01