CheckBox怎么在Android中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

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);
//通過(guò)設(shè)置checkbox的監(jiān)聽(tīng)事件,判斷checkbox是否被選中
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 通過(guò)這個(gè)方法,來(lái)監(jiān)聽(tīng)當(dāng)前的checkbox是否被選中
if (isChecked) {
//獲得checkBox的文本內(nèi)容
String text = checkBox.getText().toString();
Toast.makeText(MainActivity.this, "111", 1).show();
}
}
});
}
}看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享標(biāo)題:CheckBox怎么在Android中使用-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://chinadenli.net/article14/dpijde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、面包屑導(dǎo)航、Google、標(biāo)簽優(yōu)化、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容