2017-01-15 1 views
0

안녕하세요 모든 첫 번째 일이 하루 종일 대답을 찾고 있었고 아무것도 찾지 못했습니다. 배경색과 "점"을 저장해야하는 간단한 것을 저장하려고합니다. 사용자가 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에

+0

무엇을 sp에 저장 하시겠습니까? – W4R10CK

+0

@ W4R10CK 내가 말했듯이 "앱을 닫고 돌아가서 앱이 사용자가 선택한 마지막 bg 색상을 보여 주며 +1 버튼이 클릭 된 시간을 알려줍니다." –

답변

0

변경에게 SharedPreference 비트 도움 :에 SharedPreference 비트를 변경

SharedPreferences.Editor editor = getSharedPreferences(save, MODE_PRIVATE).edit(); 
editor.putString("textValue",tv.getText().toString()); 
editor.commit(); 

그리고 한 OnCreate에서 :

SharedPreferences prefs = getSharedPreferences(save, MODE_PRIVATE); 
String textValue= prefs.getString("textValue", "0"); 

그리고 당신이 가진 후이 코드를 추가 tv를 TextView로 선언했습니다. 배경 색상을 변경하기위한 각 버튼에

tv.setText(textValue) 

, 'RED'를이 코드를 넣어 'BLUE'내 변수 'COLOR'를 변경 등

SharedPreferences.Editor editor = getSharedPreferences(saveColour, MODE_PRIVATE).edit(); 
editor.putString("colourValue",COLOUR); 
editor.commit(); 

그런 다음에 생성이 전화 :

SharedPreferences prefs = getSharedPreferences(saveColour, MODE_PRIVATE); 
String colourValue = prefs.getString("colourValue", WHITE); 

그리고 당신은 mLinearLayout 선언 한 후 :

mlinearLayout.setBackgroundColor(Color.colourValue); 
+0

시간 동생이 아니라 여전히 afther 내가 애플 리케이션을 닫고 TV 쇼 0을 반환하고 닫기 내가 버튼에서 7 시간 preas을 –

+0

당신은 '포인트'변수에 0 값을 사용하고 있습니다. 즉, btwpluse를 누르면 단지 0이 추가됩니다. ?. 숫자를 0에서 1로 변경하십시오. –

+0

또한 코드는 실제로 tv를 SharedPreference 변수로 변경하고 있음을 나타내지 않습니다. 나는 이것을 보여주기 위해 나의 답을 수정했다. –

관련 문제