스위치 on/off에 따라 나타나고 사라지게 할 위젯들을 하나의 LinearLayout 밑에 넣어서 코드를 간결히 하였군요.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:text="@string/v_start" | |
android:id="@+id/textView" /> | |
<Switch | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/v_started" | |
android:id="@+id/switch1" | |
android:textSize="20dp" | |
android:checked="false" | |
android:showText="true" /> | |
<LinearLayout | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/linear1" | |
android:visibility="invisible"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:text="@string/s_version" | |
android:id="@+id/textView_android" /> | |
<RadioGroup | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/radioGroup1" | |
> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/v_jellybean" | |
android:id="@+id/radioButton_jelly" /> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/v_kitkat" | |
android:id="@+id/radioButton_kitkat" /> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/v_lollipop" | |
android:id="@+id/radioButton_lolli" /> | |
<ImageView | |
android:layout_width="200dp" | |
android:layout_height="180dp" | |
android:id="@+id/imageView" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/v_exit" | |
android:onClick="OnClick" | |
android:id="@+id/button_exit" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/v_restart" | |
android:onClick="OnClick" | |
android:id="@+id/button_restart" /> | |
</RadioGroup> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
Button bnt_exit, bnt_restart; | |
ImageView i_image; | |
Switch switch_agree; | |
RadioGroup rGroup; | |
RadioButton rd_jelly, rd_kit, rd_lolli; | |
LinearLayout linear1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
bnt_exit = (Button) findViewById(R.id.button_exit); | |
bnt_restart = (Button) findViewById(R.id.button_restart); | |
i_image = (ImageView) findViewById(R.id.imageView); | |
switch_agree = (Switch) findViewById(R.id.switch1); | |
rGroup = (RadioGroup) findViewById(R.id.radioGroup1); | |
rd_jelly = (RadioButton) findViewById(R.id.radioButton_jelly); | |
rd_kit = (RadioButton) findViewById(R.id.radioButton_kitkat); | |
rd_lolli = (RadioButton) findViewById(R.id.radioButton_lolli); | |
linear1 = (LinearLayout) findViewById(R.id.linear1); | |
switch_agree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if (switch_agree.isChecked() == true) { | |
linear1.setVisibility(View.VISIBLE); | |
} else { | |
linear1.setVisibility(View.INVISIBLE); | |
} | |
} | |
}); | |
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(RadioGroup group, int checkedId) { | |
switch (checkedId) { | |
case R.id.radioButton_jelly: | |
i_image.setImageResource(R.drawable.jellybean_); | |
break; | |
case R.id.radioButton_kitkat: | |
i_image.setImageResource(R.drawable.kitkat_); | |
break; | |
case R.id.radioButton_lolli: | |
i_image.setImageResource(R.drawable.lollipop_); | |
break; | |
} | |
} | |
}); | |
} | |
public void OnClick(View v){ | |
switch(v.getId()){ | |
case R.id.button_exit: | |
finish(); | |
break; | |
case R.id.button_restart: | |
Intent i = getBaseContext().getPackageManager() | |
.getLaunchIntentForPackage( getBaseContext().getPackageName() ); | |
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
startActivity(i); | |
break; | |
} | |
} | |
} |