안녕하세요 모든 첫 번째 일이 하루 종일 대답을 찾고 있었고 아무것도 찾지 못했습니다. 배경색과 "점"을 저장해야하는 간단한 것을 저장하려고합니다. 사용자가 button
에있는 사용자는 을 변경하고 을 누르고 textview
에 +1을 넣은 button
을 입력해야한다고 설명합니다.SharedPreferences 텍스트보기 및 bg 색상을 저장하는 방법
내가 응용 프로그램을 닫고 난 앱이 +1 button
클릭 마지막 배경은 사용자가 선택한 색상과 방법, 많은 시간이 표시됩니다 것을 원하는 반환 후 : 여기
코드 : XML을 :
package com.example.hanansanag.colorbeck;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnred,btngreen,btnblue,btnpluse,btnsave;
private LinearLayout mlinearLayout;
private TextView tv;
int point = 0;
public SharedPreferences sp;
private int progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp= getSharedPreferences("save",0);
String textValue = sp.getString("textvalue","");
mlinearLayout=(LinearLayout)findViewById(R.id.activity_main);
btnred = (Button) findViewById(R.id.cred);
btnblue = (Button) findViewById(R.id.cblue);
btngreen = (Button) findViewById(R.id.cgreen);
btnpluse = (Button)findViewById(R.id.btnadd);
btnsave = (Button)findViewById(R.id.save);
tv=(TextView)findViewById(R.id.tv) ;
btnred.setOnClickListener(this);
btnsave.setOnClickListener(this);
btnpluse.setOnClickListener(this);
btnblue.setOnClickListener(this);
btngreen.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (btnred == v){
mlinearLayout.setBackgroundColor(Color.RED);
}
else if (btngreen == v){
mlinearLayout.setBackgroundColor(Color.GREEN);
}
else if (btnblue == v){
mlinearLayout.setBackgroundColor(Color.BLUE);
}
else if (btnpluse== v){
point++;
tv.setText("" + point);
}
else if (btnsave == v){
Toast.makeText(this,"btn clickd",Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor = sp.edit();
editor.putString("textValue",tv.getText().toString());
editor.commit();
}
}
}
: 여기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hanansanag.colorbeck.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="home work :)"
android:layout_gravity="center"
android:textSize="20sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change bg :"
android:textStyle="bold"
android:textSize="15sp"
android:id="@+id/Tv2"
android:textAllCaps="false"/>
<Button
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:id="@+id/cred"
android:text="red"
android:background="@drawable/redbutton"
android:textAllCaps="false"/>
<Button
android:layout_width="80dp"
android:layout_height="50dp"
android:text="green"
android:id="@+id/cgreen"
android:layout_marginLeft="10dp"
android:background="@drawable/greenbtm"
android:textAllCaps="false"/>
<Button
android:layout_width="80dp"
android:layout_height="50dp"
android:background="@drawable/blue222"
android:id="@+id/cblue"
android:layout_marginLeft="10dp"
android:text="blue"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/easyyyyy"
android:id="@+id/btnadd"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/tv"
android:textSize="20sp"
android:layout_marginLeft="70dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="70dp"
android:id="@+id/save"
android:layout_height="70dp"
android:layout_marginLeft="110dp"
android:background="@drawable/save"/>
</LinearLayout>
</LinearLayout>
및는 자바 코드
당신은 내가 여기에서 시도한 것을 볼 수있다 Textview
위에서 SharedPreferences
그러나 내가 닫고 돌아오고 난 후에 나는 내가 무엇인가 잘못 안다는 것을 알지 못한다. 그러나 나는 무엇을 모른다. 모든 시간 타이와에 btnsave에
무엇을 sp에 저장 하시겠습니까? – W4R10CK
@ W4R10CK 내가 말했듯이 "앱을 닫고 돌아가서 앱이 사용자가 선택한 마지막 bg 색상을 보여 주며 +1 버튼이 클릭 된 시간을 알려줍니다." –