profile image

L o a d i n g . . .

반응형


안드로이드 개인 세미 프로젝트 ~ 스터디 스탑워치

Android Personal SemiProject ~ Study StopWatch


설정화면을 추가 하였고, 그 설정화면에서는 배경화면을 선택할 수 있게 하였다.
아직 디자인적인 요소는 건들지 않아서 투박해 보일 수 있다.
기능이 중요하다고 생각하여 디자인적인 요소는 뒤로 미뤄 두었다.


Add the setting view, it choice a background image
The drsign element has not yet been done, so it can seem daunting
I think the function is important, the design element was put back

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="4">

<Button
android:id="@+id/center2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="@drawable/center2"
android:text="small cute"
android:textSize="8dp" />

<Button
android:id="@+id/center3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="@drawable/center3"
android:text="large cute"
android:textSize="8dp"/>

<Button
android:id="@+id/center4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="@drawable/center4"
android:text="large black"
android:textSize="8dp"/>

<Button
android:id="@+id/center5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="@drawable/center5"
android:text="word"
android:textSize="8dp"/>

</LinearLayout>

</LinearLayout>

설정화면에는 간단하게 이미지가 들어있는 버튼 4개만 추가해 놓았다.


Simply setting view has four button that have background image



public class SettingActivity extends AppCompatActivity {

private Button center2;
private Button center3;
private Button center4;
private Button center5;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);

Intent intent = new Intent(getIntent());

center2 = (Button) findViewById(R.id.center2);
center3 = (Button) findViewById(R.id.center3);
center4 = (Button) findViewById(R.id.center4);
center5 = (Button) findViewById(R.id.center5);

center2.setOnClickListener(listener);
center3.setOnClickListener(listener);
center4.setOnClickListener(listener);
center5.setOnClickListener(listener);
}

View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.center2: change_Back(R.drawable.center2);
break;
case R.id.center3: change_Back(R.drawable.center3);
break;
case R.id.center4: change_Back(R.drawable.center4);
break;
case R.id.center5: change_Back(R.drawable.center5);
break;
}

}
};

public void change_Back(int changeId) {
Intent intent = new Intent(SettingActivity.this, MainActivity.class);

intent.putExtra("id", changeId);
startActivity(intent);

finish();
}
}

처음에는 버튼 4개마다 각 리스너를 만들고 같은 코드를 4번 넣었다.

그러므로 같은 코드가 4개가 만들어 졌는데 다른건 changeId라 명명한 int값만 달랐다.

이 int는 R.drawable.center이며 이가 int값이라는 것은 오늘 처음 알게 되었다. (창피창피)

이를 하나의 "change_Back"메소드로 만들어서 코드를 줄였으며, 이 메소드에서 인텐트까지 처리하게 하였다.

처음에는 IF문을 통하여 구분하였지만, 리스너 객체를 생성하여 Switch~Case문으로 제어를 하니 더 깔끔해지고 가독성이 높아졌다.

위와 같은 과정을 잘 기억하고 있으며 나중에 버튼이 많은 리스너를 만들때 각 버튼마다 리스너를 오버라이딩 하지 않아도 될 것 같다.


At first, each four button made listener and smae code in four times.

Therefore i've gor same code in four times, the only difference was the int, which is called changeId

Int is "R.drawable.center", it was first noticed today that the tooth was worth an int (Embarrassing)

I made a method called "change_Back", thus the code was reduced, and the method was processed to intent

At first it was separated through IF statements, by creating a riner object and controlling it with Switch~Case statements, it became cleaner and more readable

I remember this process well, and i don't think i need to override it with eack button when i create a riser with a lot of buttons later.


// 배경 바꾸기
// change Background
Intent intent = new Intent();

intent = getIntent();

Log.d(TAG, "check");

String test = intent.getStringExtra("id");
time_Back.setBackground(getDrawable(intent.getIntExtra("id", R.drawable.center)));

// 설정 화면으로 이동
// go to settingview
setting_Id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
startActivity(intent);
}
});

MainActivity에서는 time_Back.setBackground(getDrawable(intent.getIntExtra("id", R.drawable.center))); 이 가장 중요한데

이 한줄의 코드로 선택된 배경화면으로 변경이 된다.

id값에는 전 액티비티에 있는 R.drawable.center2~5 들이 들어 있으므로 선택된 값으로 변경이 되며

아무것도 선택이 안되었을경우 디폴트 값인 R.drawable.centner이 지정이 되어 배경화면이 변경이 되지 않는다.


time_Back.setBackground(getDrawable(intent.getIntExtra("id", R.drawable.center))); is most important in MainActivity

The screen will be changed to a background screen selected by a single line of code

The value contains R.drawable.center2~5 in the previous activity, so the value changes to the selected value

If nothing is selected, the default value, R.drawable.center is specified and the background screen is not shanged.




반응형
복사했습니다!