【问题标题】:How do I set my checkbox checked by default after a certain action is performed?执行特定操作后,如何将复选框设置为默认选中?
【发布时间】:2022-01-24 15:32:41
【问题描述】:

我有一个带有复选框列表的活动。一个复选框,单击它后,将调用、执行并完成下一个操作。之后,操作完成,应用程序返回到带有复选框列表的操作,现在我希望默认选中使用的复选框并且无法再次单击。请帮忙!

这是我尝试执行但未成功的操作,当我返回操作时,复选框变为未选中状态:

P2106.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Intent move = new Intent(GeneralRoute.this, ScanTest.class);
                startActivity(move);
                P2106.setChecked(true);
            }
        }
    });

【问题讨论】:

  • 您可以将该数据存储在sharedPreferences 中,或者您可以使用数据库,即:Firebase 进行存储,然后您可以检查用户是否被检查。
  • 使您的列表静态化,或使用共享首选项或数据库来保存您的数据,如果您想要一种简单的方法,您必须使您的列表静态化并且在您的 setOnCheckedChangeListener 中您必须更改您的值型号
  • @OneDev 我该怎么做?请给我一个示例代码好吗?
  • 是的,我会把它作为答案发布

标签: java android checkbox default


【解决方案1】:

你的模型类

public class YourModel {

private boolean someBoolean;
private String someString;
private int someInt;

public boolean isSomeBoolean() {
    return someBoolean;
}

public void setSomeBoolean(boolean someBoolean) {
    this.someBoolean = someBoolean;
}

public String getSomeString() {
    return someString;
}

public void setSomeString(String someString) {
    this.someString = someString;
}

public int getSomeInt() {
    return someInt;
}

public void setSomeInt(int someInt) {
    this.someInt = someInt;
}
}

你的活动

public class MainActivity extends AppCompatActivity {

private static ArrayList<YourModel> modelList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Add Your Model Or Data To ArrayList
    modelList.add(new YourModel());
    modelList.add(new YourModel());

    ListView listView = findViewById(R.id.youtListView);
    YourAdapter yourAdapter = new YourAdapter(this, R.id.youtLayoutListItem, modelList);
    listView.setAdapter(yourAdapter);


}
}

和你的适配器

public class YourAdapter extends ArrayAdapter<YourModel> {

private ArrayList<YourModel> modelList;

public YourAdapter(@NonNull Context context, int resource, @NonNull List<YourModel> objects) {
    super(context, resource, objects);
    this.modelList = (ArrayList<YourModel>) objects;
}

@Override
public int getCount() {
    return modelList.size();
}

@Nullable
@Override
public YourModel getItem(int position) {
    return modelList.get(position);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (convertView == null) {
            ...
    }
    YourModel model = modelList.get(position);
    P2106.setChecked(model.isSomeBoolean());
    P2106.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Intent move = new Intent(GeneralRoute.this, ScanTest.class);
                startActivity(move);
                P2106.setChecked(true);
                model.setSomeBoolean(true);
                modelList.set(modelList.indexOf(model), model);
                notifyDataSetChanged();
            }
        }
    });
    return convertView;
}
}

别忘了用您的数据替换

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2016-09-19
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多