我有一个布局,其中有一个开关和包含 2 个图像按钮的 RelativeLayout。默认情况下,开关保持关闭状态。我想要的是当活动加载时我想将相对布局设置为不可见,如果开关设置为打开状态,那么相对布局应该是可见的。
当我尝试在我的 OnCreate 中将其设置为不可见时,它给了我 NullPointerException ..
如何在活动开始时将布局设置为不可见,如果选中了开关,那么只有布局应该是可见的..?请帮忙..
布局.xml:-
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/btnStyleBeige"
android:id="@+id/switch1"
android:layout_marginRight="10dp"
android:layout_alignTop="@+id/textView7"
android:layout_alignStart="@+id/button5" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relative2"
android:layout_marginTop="40dp"
android:layout_below="@+id/switch1"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/taskname">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pickDate"
android:src="@drawable/c2"
style="@style/btnStyleBeige"
android:layout_marginStart="65dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pickTime"
style="@style/btnStyleBeige"
android:src="@drawable/c1"
android:layout_marginEnd="65dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
活动 :-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addtask);
getActionBar().setDisplayHomeAsUpEnabled(true);
relativeLayout2.setVisibility(View.INVISIBLE);
Switch aSwitch = (Switch) findViewById(R.id.switch1);
aSwitch.setOnCheckedChangeListener(this);
init();
/** Capture our View elements */
//pDisplayDate = (TextView) findViewById(R.id.displayDate);
pPickDate = (ImageButton) findViewById(R.id.pickDate);
/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
/** Capture our View elements */
//pDisplayTime = (TextView) findViewById(R.id.displayTime);
pPickTime = (ImageButton) findViewById(R.id.pickTime);
/** Listener for click event of the button */
pPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
/** Get the current date and time */
final Calendar cal = Calendar.getInstance();
pYear = cal.get(Calendar.YEAR);
pMonth = cal.get(Calendar.MONTH);
pDay = cal.get(Calendar.DAY_OF_MONTH);
mHour = cal.get(Calendar.HOUR_OF_DAY);
mMinute = cal.get(Calendar.MINUTE);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
relativeLayout2 = (RelativeLayout)findViewById(R.id.relative2);
if(isChecked) {
relativeLayout2.setVisibility(View.GONE);
} else {
relativeLayout2.setVisibility(View.VISIBLE);
}
}