我有两个edittext块+两个按钮(使用savedpreferences),我似乎无法弄清楚如何在两者上使用onclicklistener?我现在有两个按钮,它们需要在 onclicklistener 上进行其他操作。如何更改代码以使其正常工作?
我对android真的很陌生。
主代码
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();
}