2017-01-19 4 views
-2

を保存します。SharedPreferencesがhi私は彼がコードを取得いけないwhay知っidont BGの色を保存して、問題を持っているのBG色誤差

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,prefs; 
    private int progress; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     SharedPreferences prefs = getSharedPreferences("save", MODE_PRIVATE); 
     String textValue= prefs.getString("textValue", "0"); 

     mlinearLayout=(LinearLayout)findViewById(R.id.activity_main); 
     SharedPreferences sp = getSharedPreferences("saveColour", MODE_PRIVATE); 
     String colourValue = sp.getString("colourValue", "WHITE"); 
     mlinearLayout.setBackgroundColor(Color.colourValue); 

     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) ; 
     tv.setText(textValue); 
     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); 
      SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit(); 
      editor.putString("colourValue","RED"); 
      editor.commit(); 

     } 
     else if (btngreen == v){ 
      mlinearLayout.setBackgroundColor(Color.GREEN); 
      SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit(); 
      editor.putString("colourValue","GREEN"); 
      editor.commit(); 
     } 
     else if (btnblue == v){ 
      mlinearLayout.setBackgroundColor(Color.BLUE); 
      SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit(); 
      editor.putString("colourValue","BLUE"); 
      editor.commit(); 


     } 
     else if (btnpluse== v){ 
      point++; 
      tv.setText("" + point); 

     } 
     else if (btnsave == v){ 
      Toast.makeText(this,"btn clickd",Toast.LENGTH_LONG).show(); 
      SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit(); 
      editor.putString("textValue",tv.getText().toString()); 
      editor.commit(); 
     } 
    } 
} 

をし、ここでの問題 image of the error

+0

「Color.WHITE」として使用することができます –

+0

あなたはあなたのことを理解していません –

+0

'Color。あなたの色は整数値を返し、あなたはStringを渡しています。だから私は –

答えて

1

与えられたアップデートで、あなたのコードを置き換えの画像です:私はここで、ここでのJavaコードお願いします

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,prefs; 
    private int progress; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     SharedPreferences prefs = getSharedPreferences("save", MODE_PRIVATE); 
     String textValue= prefs.getString("textValue", "0"); 

     mlinearLayout=(LinearLayout)findViewById(R.id.activity_main); 
     SharedPreferences sp = getSharedPreferences("saveColour", MODE_PRIVATE); 
     int colourValue = sp.getInt("colourValue", Color.WHITE); 
     mlinearLayout.setBackgroundColor(colourValue); 

     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) ; 
     tv.setText(textValue); 
     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); 
      SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit(); 
      editor.putInt("colourValue", Color.RED); 
      editor.commit(); 

     } 
     else if (btngreen == v){ 
      mlinearLayout.setBackgroundColor(Color.GREEN); 
      SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit(); 
      editor.putInt("colourValue", Color.GREEN); 
      editor.commit(); 
     } 
     else if (btnblue == v){ 
      mlinearLayout.setBackgroundColor(Color.BLUE); 
      SharedPreferences.Editor editor = getSharedPreferences("saveColour", MODE_PRIVATE).edit(); 
      editor.putInt("colourValue", Color.BLUE); 
      editor.commit(); 


     } 
     else if (btnpluse== v){ 
      point++; 
      tv.setText("" + point); 

     } 
     else if (btnsave == v){ 
      Toast.makeText(this,"btn clickd",Toast.LENGTH_LONG).show(); 
      SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit(); 
      editor.putString("textValue", tv.getText().toString()); 
      editor.commit(); 
     } 
    } 
} 

希望がお手伝いします!

+0

あなたのhelo秒のためのすべてのtyの最初の兄弟私は確かにim doind somthing wrong becuzまだ私の間違ったことを教えてください:https://s29.postimg.org/a3za3syhz/image.jpg https://s23.postimg.org/60srsuqjv/image.jpg –

+0

私の更新された答えを見てください! –

+0

私はまだ同じ問題を抱えていますが、あなたは "context.getSharedPreferences"を置いたように間違ったことをしていますが、彼は私に "コンテキスト"を見せてはいけません。https://s23.postimg.org/foanpk8m3/334.jpg –

関連する問題