我有两个radiobutton组需要在加载屏幕时进行初始化,所以我使用了这段代码:
mVisibilityPublic.setChecked(true);
但是当我加载屏幕时,我得到以下内容(首先是错误的行为,第二个是预期的行为):
我已尝试过所有这些:
mVisibilityPublic.setSelected(true);
mVisibilityPublic.performClick();
mVisibilityPublic.setActivated(true);
mVisibilityPublic.invalidate();
mVisibilityPublic.requestLayout();
但我得到的结果相同。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="@dimen/form_vertical_spacing"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".createevent.CreateEventActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/create_event_privacy"
android:textColor="@color/colorAccent"
android:textStyle="bold"
/>
<RadioGroup
android:id="@+id/create_visibility_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="@+id/createevent_visibility_public"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/create_event_public_event"/>
<RadioButton
android:id="@+id/createevent_visibility_private"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/create_event_private_event"/>
<RadioButton
android:id="@+id/createevent_visibility_ppv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/create_event_ppv"/>
</RadioGroup>
<LinearLayout
android:id="@+id/ppv_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="@string/create_event_ppv_section"
android:textColor="@color/colorAccent"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="@+id/ppv.currency.layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.myapp.ui.AutoCompleteCombo
android:id="@+id/ppv.currency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/create_event_ppv_currency">
</com.myapp.ui.AutoCompleteCombo>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/ppv.price.layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1">
<android.support.design.widget.TextInputEditText
android:id="@+id/ppv.price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/create_event_ppv_price"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxLines="1"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<RadioGroup
android:id="@+id/ppv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="@+id/ppv_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/create_event_ppv_allcountries"/>
<RadioButton
android:id="@+id/ppv_allexcept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/create_event_ppv_except"/>
<RadioButton
android:id="@+id/ppv_only"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/create_event_ppv_some"/>
</RadioGroup>
<com.myapp.countries.CountriesMultiCombo
android:id="@+id/create.ppv.countrylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout>
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/general_info"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="-32dp"
android:layout_marginRight="-32dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@color/colorDivider"
/>
<LinearLayout
android:id="@+id/geoblock_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="@string/geo_blocking_settings_title"
android:textColor="@color/colorAccent"
android:textStyle="bold"
/>
<RadioGroup
android:id="@+id/geo_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="@+id/geo_none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/geo_none"/>
<RadioButton
android:id="@+id/geo_blockonly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/geo_block_only"/>
<RadioButton
android:id="@+id/geo_blockexcept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/geo_block_all_except"/>
</RadioGroup>
<com.myapp.createevent.countries.CountriesMultiCombo
android:id="@+id/create.geo.countrylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout> <Button
android:id="@+id/test_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CLICK ME"/>
</LinearLayout>
</ScrollView>
请注意,如果我手动选择该选项(就像我对图像中的第二个示例所做的那样),我会得到正确的结果。只有当我以编程方式执行它时才会错过内圈。
public class TestFragment extends Fragment {
private Unbinder mUnbinder;
@BindView(R.id.createevent_visibility_public)
RadioButton mVisibilityPublic;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_advanced_opts, container, false);
mUnbinder = ButterKnife.bind(this, inflate);
return inflate;
}
public static TestFragment newInstance(int tvId, int eventId) {
TestFragment testFragment = new TestFragment();
Bundle args = new Bundle();
args.putInt(TV_ARGUMENT, tvId);
args.putInt(EVENTID_ARGUMENT, eventId);
testFragment.setArguments(args);
return testFragment;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mVisibilityPublic.setChecked(true);
}
}