我有兩個edittext塊+兩個按鈕(誰與savedpreferences一起工作),我似乎無法弄清楚如何在兩個上使用onclicklistener?我現在有兩個按鈕需要onclicklistener上的其他東西。如何修改代碼以使其工作?兩個OnClickListeners,兩個按鈕。 (保存的首選項)
我真的很陌生。
Maincode
public class MainActivity extends Activity implements OnClickListener{
EditText editText;
EditText editText1;
Button button;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.registerReceiver(this.mConnReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
editText1 = (EditText) findViewById(R.id.editText2);
button1 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
loadSavedPreferences();
loadSavedPreferences1();
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String name = sharedPreferences.getString("storedName", "YourName");
editText.setText(name);
}
private void loadSavedPreferences1() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String onderwerp = sharedPreferences.getString("storedOnderwerp", "YourOnderwerp");
editText1.setText(onderwerp);
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
private void savePreferences1(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor1 = sharedPreferences.edit();
editor1.putString(key, value);
editor1.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences("storedName", editText.getText().toString());
Toast.makeText(getApplicationContext(),
"Meldingtekst is aangepast.", Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences1("storedOnderwerp", editText1.getText().toString());
Toast.makeText(getApplicationContext(),
"Meldingonderwerp is aangepast.", Toast.LENGTH_LONG).show();
}
首先您應該嘗試瞭解接口的概念。如果你能理解它,那麼你絕不會混淆這樣的事情。 –