0

& 버튼을 onLongClick에 영구 저장합니다 (다시 변경하지 않는 한 &까지). This은 전체 코드의 스크린 샷입니다. 그러나 응용 프로그램을 닫을 때 & 다시 열면 단추 텍스트가 기본값으로 유지됩니다. 내가 실수 한거야?변경 및 저장 버튼 텍스트 onLongClick sharedpreferences를 사용합니다.

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

당신이 누락 된 활동에 대한 공유 환경 설정에서 텍스트를 가져옵니다. 솔루션

넣어이 라인 : 경우 (sharedPreferences.getString ("이름", "") .equals ("")) {

btn.setText("YES"); 

} 다른 {

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

}

이 줄 아래 : btn = (Button) findViewById (R.id.button); 내가 볼 수있는

0

주요 문제는이 라인이 : 버튼을 초기화하고 뷰에서 발견되기 전에

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

가 호출됩니다. 이로 인해 문자열이 trypatch되지 않은 경우 응용 프로그램과 충돌하는 nullpointer로 설정됩니다. 텍스트를 설정하기 전에 버튼을 정의하면 nullpointer 및 텍스트가 설정되지 않습니다.

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

이 동작을 일으키는 제어 흐름입니다.

당신이 oncreate에서 선호도에서 데이터를 가져오고 응용 프로그램을 다시 만들 때

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

그래서, 당신은 preference에서 저장 text를 찾으 button 처럼 그것을 설정하고 버튼을 설정해야하고 끝났다.

관련 문제