2012-04-15 139 views
0

帶按鈕的主要活動我想更改。它應該導入用戶什麼放在EditText上從其他活動:讓用戶輸入Android上的按鈕名稱

@Override 
protected void onResume() { 
    super.onResume(); 

    class1.setText(this.getButtonText()); 
} 

public String getButtonText() 
{ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    String buttonText = prefs.getString("ButtonText", "Default Button Test"); 
    return buttonText; 
} 

活動與編輯文本和一個按鈕,用戶返回到主頁。我試圖使用共享的喜好,但我不知道如何做到這一點?:

Button class1; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.editclass1); 

     SettingButtons(); 
     class1.setOnClickListener(this); 

} 


private void SettingButtons() { 
    // TODO Auto-generated method stub 
    class1 = (Button) findViewById(R.id.edittoclass1); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()){ 
    case R.id.edittoclass1: 
     startActivity(new Intent("com.clayton.calendar.TOCLASS")); 
    break; 

    } 
} 






protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    Editor editor = prefs.edit(); 
    editor.putString("ButtonText", // This is not working 
      ((TextView)findViewById(R.id.edittoclass1)).getText().toString()); 
    editor.commit(); 
    } 

}

回答

0

創建一個包含一個EditText一個新的活動。當用戶長時間點擊源活動中的按鈕時,請將它們帶到這個新活動中。在新活動的onPause中,將EditText的文本保存爲共享首選項中的值。然後,在源活動的onResume中,從共享首選項中獲取值,並將按鈕文本值設置爲此值。

下面是一些代碼,你需要的編輯按鈕文本活動的onPause一個例子:

@Override 
protected void onPause() { 
    super.onPause(); 

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    Editor editor = prefs.edit(); 
    editor.putString("ButtonText", ((TextView)findViewById(R.id.buttonText)).getText().toString()); 
    editor.commit(); 
} 

這裏有您需要在您的源/原活動代碼的例子:

@Override 
protected void onResume() { 
    super.onResume(); 

    button.setText(this.getButtonText()); 
} 


public String getButtonText() 
{ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    String buttonText = prefs.getString("ButtonText", "Default button text"); 
    return buttonText; 
} 
+0

這樣做,但它仍然不工作?它不會更改主要活動的按鈕名稱。我的新代碼已更改 – user1334858 2012-04-20 22:14:07

+0

EditClass1中的哪個位置可以保存EditText中的值? – 2012-04-20 22:45:19

+0

我忘了它的一部分。 – user1334858 2012-04-20 22:59:53