2012-06-06 4 views
0

나는 체크/체크하지 않은 아이콘 (png)을 사용하기 위해 몇 가지 체크 박스를 가지고있다.Android : 체크 박스의 스타일을 동적으로 지정하기

이 체크 박스는 런타임에 동적으로 생성됩니다. Java 소스 코드에 내 아이콘을 추가하려면 어떻게합니까? 당신의 도움 :)

크리스

감사

+0

당신이 3 인수 ['CheckBox'를 호출 시도 되세요 ] (http://developer.android.com/reference/android/widget/CheckBox.html#CheckBox%28android.content.Context,%20android.util.AttributeSet , % 20int % 29) 생성자? 'CheckBox'의 소스 코드를 살펴보면, 1 인자의 생성자를 호출하는 것은 내부 스타일을 가진 3 인자의 인자를 호출한다는 것입니다 :'this (context, null, com.android.internal.R.attr.checkboxStyle) ;'. 제 3의 주장으로 자신의 스타일을 시도하고 전달하는 것이 합리적이라고 말하고 싶습니다. –

답변

3

main.xml에

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#555555" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/options" 
     android:textSize="10pt" 
     android:typeface="serif" 
     android:textStyle="bold" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     /> 

    <CheckBox 
     android:id="@+id/blue_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:typeface="serif" 
     android:textStyle="bold" 

     android:text="@string/bluetooth" 
     /> 

    <CheckBox 
     android:id="@+id/wifi_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:typeface="serif" 
     android:textStyle="bold" 

     android:text="@string/wifi"  
     /> 

    <CheckBox 
     android:id="@+id/silent_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/silent" 
     android:typeface="serif" 
     android:textStyle="bold"  
     android:button="@drawable/box"/> 

    <CheckBox 
     android:id="@+id/general_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/general" 
     android:typeface="serif" 
     android:textStyle="bold"  
     android:button="@drawable/box"/> 

    <CheckBox 
     android:id="@+id/mobile_data_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/mobile_data" 
     android:typeface="serif" 
     android:textStyle="bold"  
     android:paddingLeft="80dp"  
     android:button="@drawable/switchbox"/> 

    <CheckBox 
     android:id="@+id/power_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/power_saver_mode" 
     android:typeface="serif" 
     android:textStyle="bold"  
     android:paddingLeft="80dp" 
     android:button="@drawable/switchbox"/> 

</LinearLayout> 

활동 클래스 :

package com.vimal.android.CustomCheckBox; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.Toast; 
import android.widget.CompoundButton.OnCheckedChangeListener; 

public class CustomCheckBoxActivity extends Activity { 
    private CheckBox blueSwitch; 
    private CheckBox wifiSwitch; 
    private CheckBox silentSwitch; 
    private CheckBox generalSwitch; 
    private CheckBox mobileDataSwitch; 
    private CheckBox powerSwitch; 
    String msg=""; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     blueSwitch=(CheckBox) findViewById(R.id.blue_switch); 
     wifiSwitch=(CheckBox) findViewById(R.id.wifi_switch); 
     silentSwitch=(CheckBox) findViewById(R.id.silent_switch); 
     generalSwitch=(CheckBox) findViewById(R.id.general_switch); 
     mobileDataSwitch=(CheckBox) findViewById(R.id.mobile_data_switch); 
     powerSwitch=(CheckBox) findViewById(R.id.power_switch); 

     setListener(); 
    } 

    private void setListener() { 

      blueSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         msg="BlueTooth is Switched OFF"; 
         if(isChecked){ 
          msg="BlueTooth is Switched ON"; 
         } 

         Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();       
       } 
      }); 

      wifiSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         msg="Wifi is Switched OFF"; 
         if(isChecked){ 
          msg="Wifi is Switched ON"; 
         } 

         Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();       
       } 
      }); 

      silentSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         msg="Silent Mode is Switched OFF"; 
         if(isChecked){ 
          msg="Silent Mode is Switched ON"; 
         } 

         Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();       
       } 
      }); 

      generalSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         msg="General Mode is Switched OFF"; 
         if(isChecked){ 
          msg="General Mode is Switched ON"; 
         } 

         Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();       
       } 
      }); 

      mobileDataSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         msg="Mobile Data is Switched OFF"; 
         if(isChecked){ 
          msg="Mobile Data is Switched ON"; 
         } 

         Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show();      
       } 
      }); 

      powerSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         msg="PowerSaver Mode is Switched OFF"; 
         if(isChecked){ 
          msg="PowerSaver Mode is Switched ON"; 
         } 

         Toast.makeText(CustomCheckBoxActivity.this, msg ,Toast.LENGTH_SHORT).show(); 

       } 
      });  

    } 
} 
+0

이 링크를 사용하십시오. http : //vimaltuts.com/android-tutorial-for-beginners/android-custom-checkbox –

+0

글쎄,이 경우 고정 된 숫자의 cbs가 있습니다. 하지만 제 경우에는 숫자가 런타임에 계산됩니다. –

+0

루프를 사용하여 n 개의 cbs를 만들 수 있습니다. –

관련 문제