gpt4 book ai didi

java - 无法选中复选框

转载 作者:行者123 更新时间:2023-11-30 11:30:29 25 4
gpt4 key购买 nike

我需要你的帮助来完成我的第一个 android 项目,我的目标是根据偏好设置复选框的状态,所以这是我尝试过的:

<CheckBox
android:id="@+id/checkBoxfriendRequests"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Friend requests"
android:textSize="23sp"
android:gravity="center_vertical"
android:onClick="onCheckboxClicked" />

.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState == null) {
// Add the fragment on initial activity setup
faceFrag = new FacebookFragment();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, faceFrag)
.commit();
} else {
// Or set the fragment from restored state info
faceFrag = (FacebookFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}

ActionBar myActionBar = getSupportActionBar();
myActionBar.setHomeButtonEnabled(false);

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout_face);



// get view
LayoutInflater layoutInflater = LayoutInflater.from(FacebookActivity.this);
final View v = layoutInflater.inflate(R.layout.activity_facebook, null);

// set sharedPrefernces editor
facePrefs = this.getPreferences(MODE_PRIVATE);
editor = getPreferences(MODE_PRIVATE).edit();

// checkBoxfriendRequests status
checkBoxfriendRequests = (CheckBox) v.findViewById(R.id.checkBoxfriendRequests);



if(facePrefs.getBoolean("friendRequest", false)){
checkBoxfriendRequests.setChecked(true);
Log.d("sami","checkbox is cheked");
}
else{
checkBoxfriendRequests.setChecked(false);
Log.d("sami","checkbox is uncheked");
}


}


public void onCheckboxClicked(View view) {

boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkBoxfriendRequests :
// add to preferences
editor.putBoolean("friendRequest", checked );
editor.commit();
if(checked)
Log.d("sami", " checked true" );
else
Log.d("sami", " checked false" );

break;
.....
}

}

在创建 Activity 时,我可以在 LogCat 中看到该复选框已正确选中/未选中,但从视觉上看,无论首选项参数是什么,它始终未选中。

最佳答案

确保您没有意外禁用您的复选框:

checkbox.setEnabled(false);

当我打算将其设置为未选中时,这是我的错误:

checkbox.setChecked(false);

关于java - 无法选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17778655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com