0

&保存ボタンのテキストをonLongClickに永久に(変更しない限り&まで)変更します。 Thisは全体的なコードのスクリーンショットです。しかし、私がアプリケーション&を再び閉じると、ボタンのテキストはデフォルトのままです。私は何をしているのですか?sharedpreferencesを使用してonLongClickを変更して保存ボタンテキスト

package com.demo.buttontextedit; 

import android.content.Context; 
import android.content.DialogInterface; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    SharedPreferences sharedPreferences; 
    private Button btn; 
    private EditText edit; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btn = (Button) findViewById(R.id.button); 
     edit = (EditText) findViewById(R.id.edit_text); 
     btn.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       showDialog(edit.getText().toString()); 
       return true; 
      } 
     }); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(), "You clicked Button" + 
         btn.getText().toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    private void showDialog(String str) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("input text"); 
     View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null); 
     final EditText edit_dialog = (EditText) view.findViewById(R.id.edit_dialog); 
     edit_dialog.setText(str); 
     builder.setView(view); 

     builder.setNegativeButton("cancel", null); 
     builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       sharedPreferences = getSharedPreferences("myPref", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putString("Name", edit_dialog.getText().toString()); 
       editor.commit(); 
       btn.setText(sharedPreferences.getString("Name", "")); 
      } 
     }); 
     builder.show(); 
    } 
} 
+0

sharedpreferenceの値に従って、ボタンのテキストを 'onCreate()'から更新する必要があります。 – Rashin

答えて

3

あなたはOncreteアクティビティの共有設定からテキストを取得します。 (sharedPreferences.getString( "名前"、 "").equals( "")){

btn.setText("YES"); 

}他{

btn.setText(sharedPreferences.getString("Name", "YES")); 

場合 :この行を入れ ソリューション

}

以下の行: btn =(ボタン)findViewById(R.id.button);

0

私が見ることができるように主な問題は、この行ということです:ボタンが初期化され、ビューで発見された前

btn.setText(sharedPreferences.getString("Name", "YES")); 

が呼び出されます。これにより、文字列がnullpointerに設定されます(try-catchされていない場合)、アプリケーションがクラッシュします。テキストを設定する前にボタンを定義する限り、nullpointerとテキストは設定されません。

btn = (Button) findViewById(R.id.button); 
btn.setText(sharedPreferences.getString("Name", "YES")); 
1

この動作を引き起こす制御の流れ。

アプリケーションが再作成されたときに、あなたがpreferenceから保存されたtextを見つけると、ボタンの上に置きますoncreateに好みからデータを取得し、

oncreate(Bundle b) { 
// initialize preference and layout 
String btntext=sharedPreferences.getString("btnkey","Yes") 
btn.setText(btntext); 
} 

のようなbutton にそれを設定する必要があり、完了しました。

関連する問題