import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
//선언문들
private int counter;
private TextView TX;
private ImageView IMG;
//sharedPrefaerences 선언
SharedPreferences mPref;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//변수선언
mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
initView();
//리스너 생성
IMG.setOnClickListener(new View.OnClickListener() {
//counter 초기값설정
int counter = 101;
@Override
public void onClick(View view) {
//counter -1 계산값
TX.setText(String.valueOf(--counter));
//각 상황별 조건문
switch (counter) {
case 70:
IMG.setImageResource(R.drawable.image02);
break;
case 30:
IMG.setImageResource(R.drawable.image03);
break;
case 0:
counter = counter + 101;
IMG.setImageResource(R.drawable.image01);
break;
}
}
});
}
//뷰모음
private void initView() {
TX = (TextView) findViewById(R.id.tx1);
IMG = (ImageView) findViewById(R.id.imageView1);
String callValue = mPref.getString("count", "100");
TX.setText(callValue);
}
//앱종료시 저장
@Override
protected void onDestroy() {
super.onDestroy();
SharedPreferences.Editor editor = mPref.edit();
editor.putString("count", TX.getText().toString());
editor.putString("img", IMG.getResources().toString());
editor.commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
android:background="#ffffff"
tools:context="com.example.lee.exam001.MainActivity">
<LinearLayout
android:layout_width="382dp"
android:layout_height="508dp"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="30sp"
android:id="@+id/tx1"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="251dp"
android:src="@drawable/image01"
android:clickable="true"
android:padding="10px"
android:maxWidth="70px"
android:maxHeight="70px"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
앱에 뜨는 그림터치시, 100부터 0까지 카운터가 내려가고 각각 70,30때 그림이변경되고
카운트가 0이되고난 후 앱 제일첫그림으로 돌아가고
카운트중 종료후 앱재실행하면 이전에있던 카운터가 남아있어야하고, 그림도 남아야있어야함
여기서질문!
-종료후 다시 실행하면 카운트 숫자는 남아있으나 1회터치하면 다시 100으로 돌아갑니다
-사진은 어떻게 Sharedpreferences로 저장해서 불러오나요
(2번째 사진에서 종료되면 다시 켰을떄 2번째 사진이 뜨는것)