Android自定義CheckBox含代碼

    Android自定義CheckBox

    Android提供了自定義視圖元素的界面的功能,而不是默認的界面。愛掏網 - it200.com

    您可以在Android中創建自定義的CheckBox。愛掏網 - it200.com因此,您可以在布局中添加一些不同的復選框圖片。愛掏網 - it200.com

    自定義CheckBox示例

    在此示例中,我們創建了默認的復選框和自定義的復選框。愛掏網 - it200.com將以下代碼添加到activity_main.xml文件中。愛掏網 - it200.com

    activity_main.xml

    文件:activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:app="http://schemas.android.com/apk/res-auto"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        tools:context="example.javatpoint.com.customcheckbox.MainActivity">  
    
    
        <TextView  
            android:id="@+id/textView1"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:gravity="center_horizontal"  
            android:textSize="25dp"  
            android:text="Default Check Box"  
            android:layout_alignParentTop="true"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentStart="true" />  
    
        <CheckBox  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="New CheckBox"  
            android:id="@+id/checkBox"  
            android:layout_below="@+id/textView1"  
            android:layout_centerHorizontal="true"  
            android:layout_marginTop="46dp" />  
    
        <CheckBox  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="New CheckBox"  
            android:id="@+id/checkBox2"  
            android:layout_below="@+id/checkBox"  
            android:layout_alignLeft="@+id/checkBox"  
            android:layout_alignStart="@+id/checkBox" />  
    
        <View  
            android:layout_width="fill_parent"  
            android:layout_height="1dp"  
            android:layout_marginTop="200dp"  
            android:background="#B8B894"  
            android:id="@+id/viewStub" />  
    
        <CheckBox  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="CheckBox 1"  
            android:id="@+id/checkBox3"  
            android:button="@drawable/customcheckbox"  
            android:layout_below="@+id/viewStub"  
            android:layout_centerHorizontal="true"  
            android:layout_marginTop="58dp" />  
    
        <CheckBox  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="CheckBox 2"  
            android:id="@+id/checkBox4"  
            android:button="@drawable/customcheckbox"  
            android:layout_below="@+id/checkBox3"  
            android:layout_alignLeft="@+id/checkBox3"  
            android:layout_alignStart="@+id/checkBox3" />  
    
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:textSize="25dp"  
            android:text="Custom Check Box"  
            android:id="@+id/textView"  
            android:layout_alignTop="@+id/viewStub"  
            android:layout_centerHorizontal="true" />  
    
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="Show Checked"  
            android:id="@+id/button"  
            android:layout_alignParentBottom="true"  
            android:layout_centerHorizontal="true" />  
    
    </RelativeLayout>  
    

    現在在drawable文件夾下的另一個文件(checkbox.xml)中實現一個選擇器,自定義復選框。愛掏網 - it200.com

    checkbox.xml

    文件:checkbox.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <selector xmlns:android="http://schemas.android.com/apk/res/android">  
        <item android:state_checked="true" android:drawable="@drawable/checked" />  
        <item android:state_checked="false" android:drawable="@drawable/unchecked"/>  
    </selector>  
    

    Activity類

    文件:MainActivity.java

    package example.javatpoint.com.customcheckbox;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        CheckBox cb1,cb2;
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            cb1=(CheckBox)findViewById(R.id.checkBox3);
            cb2=(CheckBox)findViewById(R.id.checkBox4);
            button=(Button)findViewById(R.id.button);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StringBuilder sb=new StringBuilder("");
    
                    if(cb1.isChecked()){
                        String s1=cb1.getText().toString();
                        sb.append(s1);
                    }
    
                    if(cb2.isChecked()){
                        String s2=cb2.getText().toString();
                        sb.append("\n"+s2);
    
                    }
                    if(sb!=null && !sb.toString().equals("")){
                        Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_LONG).show();
    
                    }
                    else{
                        Toast.makeText(getApplicationContext(),"Nothing Selected", Toast.LENGTH_LONG).show();
                    }
    
                }
    
            });
        }
    }
    

    輸出

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 视频一区在线播放| 亚洲视频一区在线播放| 国产香蕉一区二区三区在线视频| 精品在线一区二区| 色狠狠色狠狠综合一区| 亚洲欧洲∨国产一区二区三区| 精品一区二区三区在线视频观看| 91video国产一区| 亚洲熟女乱综合一区二区| 国产精品成人一区二区三区| 一区二区三区在线观看| 亚洲午夜电影一区二区三区| 亚洲av无码一区二区三区网站| 中文字幕久久亚洲一区| 中文字幕日韩一区二区三区不| av在线亚洲欧洲日产一区二区| 国产伦精品一区二区三区精品| 国模极品一区二区三区| 国产精品亚洲一区二区在线观看| 亚洲av乱码一区二区三区香蕉 | 小泽玛丽无码视频一区| 蜜桃传媒一区二区亚洲AV| 亚洲一区二区三区高清在线观看 | 人成精品视频三区二区一区 | 在线精品亚洲一区二区三区| 一区二区高清在线| 无码人妻久久一区二区三区蜜桃| 一区 二区 三区 中文字幕| 深夜福利一区二区| 色噜噜AV亚洲色一区二区| 精品国产一区二区三区免费看| 亚洲AV成人一区二区三区观看| 激情亚洲一区国产精品| 无码丰满熟妇浪潮一区二区AV| 福利一区在线视频| 红杏亚洲影院一区二区三区 | 国产福利一区二区三区在线视频| 国产精品一级香蕉一区| 无码精品不卡一区二区三区 | 无码人妻一区二区三区在线| 麻豆高清免费国产一区|