如何设置有条件的导航视图?

时间:2018-08-16 10:34:40

标签: java android android-imagebutton android-navigationview

我在android中都有两个导航视图。如下面的链接图像所示,左侧为注册为父母的用户,右侧为注册为学费提供者的用户。例如,如果用户A注册为父母,则他/她只能打开左侧导航。我应该写什么编码来确保他/她是每个导航视图的用户。有人可以帮忙或教我吗?

我的界面示例

enter image description here

我正在尝试此编码:-

userCategory = (RadioGroup) findViewById(R.id.user_radio_group_btn);
    Integer checkedID = userCategory.getCheckedRadioButtonId();
    radioButton = (RadioButton) findViewById(checkedID);
    String selection = radioButton.getText().toString();

    if (selection.equals("Parents"))
    {
        menuRight.setVisibility(View.GONE);
        tuitionProvider.setVisibility(View.GONE);
    }

    if (selection.equals("Tuition Provider"))
    {
        menuLeft.setVisibility(View.GONE);
        parents.setVisibility(View.GONE);
    }

    else
    {
        Intent RegistrationIntent = new Intent(home.this, RegistrationActivity.class);
        startActivity(RegistrationIntent);
        Toast.makeText(this, "Please register first.", Toast.LENGTH_SHORT).show();
    }

XML编码:-

 <RadioGroup
    android:id="@+id/user_radio_group_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/parents_radio_button"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="50dp"
        android:layout_marginTop="175dp"
        android:background="@drawable/roundedbutton"
        android:text="Parents" />

    <RadioButton
        android:id="@+id/tuition_provider_radio_button"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="35dp"
        android:layout_marginTop="175dp"
        android:background="@drawable/roundedbutton"
        android:text="Tuition Provider" />
</RadioGroup>

我出错了

由于:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.lang.Object.equals(java.lang.Object)'         在com.addaj.mobilerecommendationsystem.home.onCreate(home.java:59)

3 个答案:

答案 0 :(得分:1)

解决方案:

代替:

Integer checkedID = userCategory.getCheckedRadioButtonId();

此处,userCategory是一个单选按钮。使用这个:

Integer checkedID = (enter here your RadioGroupId).getCheckedRadioButtonId();

尝试一下是否有效。

答案 1 :(得分:0)

检查谁已登录,仅使您对用户不可见一个NavigationDrawer,对您来说第二个可见。     例如,您选中userCategory,如果它是父级,则使教师的导航抽屉不可见

class MyModel(models.Model):

    class Meta:
        db_constraints = {
            'age_check': 'CHECK (age_from IS NULL OR age_to IS NULL OR age_from <= age_to)',
        }

答案 2 :(得分:0)

我已经测试过您的代码,您需要使用 android:checked =“ true” 在XML中设置选中单选按钮。

您需要设置至少一个选中的RadioButton,如

  

android:checked =“ true”

赞:

<RadioGroup
        android:id="@+id/user_radio_group_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/parents_radio_button"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:checked="true"
            android:text="Parents" />

        <RadioButton
            android:id="@+id/tuition_provider_radio_button"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="Tuition Provider" />
    </RadioGroup>