我一直在与这个问题作斗争超过一天,我尝试的每一个解决方案都无法为我工作。基本上我需要的是:我有一个包含复选框和textview的列表视图。在用户点击复选框时,它会被选中/取消选中。它完美地运作。但是复选框有点小,所以当用户点击相应的文本视图时我希望它们被检查,我该怎么做?
这是我的代码:
public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
Button m = (Button) findViewById(R.id.button3);
tv = (TextView) findViewById(R.id.textViewcat);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);
String[] listArray = new String[] { "All", "Friends & Family", "Sports", "Outside", "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);
Boolean[] checkedStatus = new Boolean[listArray.length];
for ( int index = 0; index < checkedStatus.length; index++)
checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);
ListView listView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
listView.setAdapter(adapter);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation8, R.anim.animation7);
}
}
MyAdapter:
public class MyAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener{
String[] values;
Boolean[] checkedStatus;
public MyAdapter(Context context, int resource, String[] values, Boolean[] checkedStatus) {
super(context, resource, values);
this.values = values;
this.checkedStatus = checkedStatus;
}
@Override
public int getCount() {
return values.length;
}
@Override
public String getItem(int position) {
return values[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, null);
}
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(values[position]);
CheckBox box = (CheckBox) view.findViewById(R.id.chk);
box.setTag(position);
box.setOnCheckedChangeListener(this);
box.setChecked(checkedStatus[position]);
return view;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Integer index = (Integer) buttonView.getTag();
checkedStatus[index] = isChecked;
String key = buttonView.getTag().toString();
SharedPreferences sharedPreferences=getContext().getSharedPreferences("status", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean(key,isChecked);
editor.apply();
}
}
答案 0 :(得分:1)
我建议您在用户点击列表项时选中/取消选中该复选框。这对我来说听起来更合适。因此,在适配器的getView()
方法中,您需要添加以下代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, null);
}
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(values[position]);
CheckBox box = (CheckBox) view.findViewById(R.id.chk);
box.setTag(position);
box.setOnCheckedChangeListener(this);
box.setChecked(checkedStatus[position]);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on each click change the state of checkbox
box.setChecked(!box.isChecked());
}
});
return view;
}
答案 1 :(得分:0)
在MyAdpater
写入单击侦听器中进行父布局或textview,然后单击“只检查当前”复选框是否已选中并按照执行操作进行复选。
e.g。
LinearLayout ll (parent layout)
CheckBox cb;
ll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(cb.isChecked())
{
code for unchecked checkbox
}
else
{
code for checked checkbox
}
}
});
答案 2 :(得分:0)
您应该使用CheckedTextView代替TextView
和CheckBox
。使用这个单独的小部件,您可以获得这种效果。
编辑:
Activity
public class MainActivity extends Activity {
ListView myListView;
Button getResult;
private ArrayList<String> dayOfWeekList = new ArrayList<String>();
private void initDayOfWeekList(){
dayOfWeekList.add("Sunday");
dayOfWeekList.add("Monday");
dayOfWeekList.add("Tuesday");
dayOfWeekList.add("Wednesday");
dayOfWeekList.add("Thursday");
dayOfWeekList.add("Friday");
dayOfWeekList.add("Saturday");
}
MyArrayAdapter myArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDayOfWeekList();
setContentView(R.layout.activity_main);
myListView = (ListView)findViewById(R.id.list);
myArrayAdapter = new MyArrayAdapter(
this,
R.layout.row,
android.R.id.text1,
dayOfWeekList
);
myListView.setAdapter(myArrayAdapter);
myListView.setOnItemClickListener(myOnItemClickListener);
getResult = (Button)findViewById(R.id.getresult);
getResult.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String result = "";
//getCheckedItems
List<String> resultList = myArrayAdapter.getCheckedItems();
for(int i = 0; i < resultList.size(); i++){
result += String.valueOf(resultList.get(i)) + "\n";
}
myArrayAdapter.getCheckedItemPositions().toString();
Toast.makeText(
getApplicationContext(),
result,
Toast.LENGTH_LONG).show();
}});
}
OnItemClickListener myOnItemClickListener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
myArrayAdapter.toggleChecked(position);
}};
private class MyArrayAdapter extends ArrayAdapter<String>{
private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();
public MyArrayAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
for(int i = 0; i < objects.size(); i++){
myChecked.put(i, false);
}
}
public void toggleChecked(int position){
if(myChecked.get(position)){
myChecked.put(position, false);
}else{
myChecked.put(position, true);
}
notifyDataSetChanged();
}
public List<Integer> getCheckedItemPositions(){
List<Integer> checkedItemPositions = new ArrayList<Integer>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItemPositions).add(i);
}
}
return checkedItemPositions;
}
public List<String> getCheckedItems(){
List<String> checkedItems = new ArrayList<String>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItems).add(dayOfWeekList.get(i));
}
}
return checkedItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1);
checkedTextView.setText(dayOfWeekList.get(position));
Boolean checked = myChecked.get(position);
if (checked != null) {
checkedTextView.setChecked(checked);
}
return row;
}
}
}
布局activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/getresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Result"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
布局row
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_vertical" />
截图: