android常用組件之Checkbox

來源:互聯網
上載者:User

標籤:

在android應用開發中,當用到多選框時,可以通過CheckBox組件實現。

該執行個體實現的功能是,挑選清單內的選項,點擊提交按鈕後,彈出對話方塊,顯示提交內容。

直接上代碼。

首先是布局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="main.testa9.MainActivity" >    <TextView         android:id="@+id/tv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/choose"/>    <CheckBox         android:id="@+id/cb1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/check_one"/>    <CheckBox         android:id="@+id/cb2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/check_two"/>    <CheckBox         android:id="@+id/cb3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/check_three"/>    <Button         android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/btn1"/></LinearLayout>

其次是strings.xml檔案:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">TestA9</string>    <string name="btn1">提交</string>     <string name="check_one">NO.1</string>    <string name="check_two">NO.2</string>    <string name="check_three">NO.3</string>    <string name="choose">請選擇:</string></resources>

最後是android源檔案:

package main.testa9;import android.support.v7.app.ActionBarActivity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;public class MainActivity extends ActionBarActivity implements OnClickListener{    private Button btn1=null;    private CheckBox cb1=null;    private CheckBox cb2=null;    private CheckBox cb3=null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);               btn1=(Button)findViewById(R.id.button1);        cb1=(CheckBox)findViewById(R.id.cb1);        cb2=(CheckBox)findViewById(R.id.cb2);        cb3=(CheckBox)findViewById(R.id.cb3);        btn1.setOnClickListener(MainActivity.this);    }    @Override    public void onClick(View arg0) {        String str="";        if(cb1.isChecked()) str="No.1";        if(cb2.isChecked()) str+="No.2";        if(cb3.isChecked()) str+="No.3";                    new AlertDialog.Builder(MainActivity.this).setTitle("提示").setMessage("您的選擇是:\n"+str).setPositiveButton("確定",new DialogInterface.OnClickListener() {                @Override            public void onClick(DialogInterface arg0, int arg1) {                //可以在該處添加按鈕事件監聽,這裡直接return,直接將該參數設定成null也可以達到相同效果                return;            }        }).show();    }   }

測試結果:

android常用組件之Checkbox

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.