一、Android中自定義checkbox樣式:html
1.首先在drawable文件夾中添加drawable文件checkbox_style.xml。android
<?xml version="1.0" encoding="utf-8"?>app
<selector xmlns:android="http://schemas.android.com/apk/res/android">ide
<item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>佈局
<item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>this
<item android:drawable="@drawable/checkbox_normal"/>spa
</selector>.net
2.在values文件夾下的styles.xml文件中添加CustomCheckboxTheme樣式。code
<style name="CustomCheckboxTheme" parent="@android :style/Widget.CompoundButton.CheckBox">orm
<item name="android:button">@drawable/checkbox_style</item>
</style>
3.在佈局文件中使用CustomCheckboxTheme樣式。
<CheckBox
android:id="@+id/select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomCheckboxTheme" />
使用到的圖片資源
checkbox_normal.png
checkbox_pressed.png
原文地址:http://blog.csdn.net/zuolongsnail/article/details/7106586
我是新來這個多重選擇 listview
。我想要保存的選中的狀態 checkbox
在 listview
,若是用戶關閉應用程序,而後再次,打開所選 checkbox
仍保持選中狀態。有任何的方式作到這一點。我尋找它,發現它可使用 SharedPreference 但我沒有獲得有關如何使用它的詳細信息。謝謝你
public class MainActivity extends AppCompatActivity { ListView myList; Button getChoice; String[] listContent = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myList = (ListView)findViewById(R.id.list); getChoice = (Button)findViewById(R.id.getchoice); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listContent); myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); myList.setAdapter(adapter); getChoice.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String selected = ""; int cntChoice = myList.getCount(); SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions(); for(int i = 0; i < cntChoice; i++){ if(sparseBooleanArray.get(i)) { selected += myList.getItemAtPosition(i).toString() + "\n"; } } Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show(); } }); } }
你能夠在保存狀態,例如, SharedPreferences
。
因此你 onCreate
和 onDestroy
方法將會看起來像︰
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPrefs", MODE_PRIVATE); @Override protected void onCreate(final Bundle savedInstanceState) { ... Set<String> checkedItemsSource = sharedPreferences.getStringSet("checked_items", new HashSet<String>()); SparseBooleanArray checkedItems = convertToCheckedItems(checkedItemsSource); for (int i = 0; i < checkedItems.size(); i++) { int checkedPosition = checkedItems.keyAt(i); listView.setItemChecked(checkedPosition, true); } } @Override protected void onDestroy() { super.onDestroy(); SparseBooleanArray checkedItems = listView.getCheckedItemPositions(); Set<String> stringSet = convertToStringSet(checkedItems); sharedPreferences.edit() .putStringSet("checked_items", stringSet) .apply(); } private SparseBooleanArray convertToCheckedItems(Set<String> checkedItems) { SparseBooleanArray array = new SparseBooleanArray(); for(String itemPositionStr : checkedItems) { int position = Integer.parseInt(itemPositionStr); array.put(position, true); } return array; } private Set<String> convertToStringSet(SparseBooleanArray checkedItems) { Set<String> result = new HashSet<>(); for (int i = 0; i < checkedItems.size(); i++) { result.add(String.valueOf(checkedItems.keyAt(i))); } return result; }
http://www.itstrike.cn/Question/4e262f3c-cc4a-4968-abe8-3f0675098f63.html