2013-10-29 48 views
0

我有兩個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(); 
} 
+0

首先您應該嘗試瞭解接口的概念。如果你能理解它,那麼你絕不會混淆這樣的事情。 –

回答

1

試試下面的代碼:

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

    if(v==button) 
    { 
     savePreferences("storedName", editText.getText().toString());  
     Toast.makeText(getApplicationContext(), "Meldingtekst is aangepast.", Toast.LENGTH_LONG).show(); 
    } 

    else if(v==button1)  
    {  
     savePreferences1("storedOnderwerp", editText1.getText().toString());  
     Toast.makeText(getApplicationContext(), "Meldingonderwerp is aangepast.", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

工作!當我被允許時,我會給它正確的答案符號。 – user2883477

+0

當然!樂於幫助。 –

0

試試這個:

button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 

       Intent activityChangeIntent = new Intent(MyActivity.this, NextActivity.class); 

       startActivity(activityChangeIntent); 


      } 
     }); 
0

你需要重寫的onClick()方法只有一次。像這樣寫:

@Override 

公共無效的onClick(視圖v){

if(v == button) 
{ 
savePreferences("storedName", editText.getText().toString()); 

Toast.makeText(getApplicationContext(), 
     "Meldingtekst is aangepast.", Toast.LENGTH_LONG).show(); 
    } 
    else if(v == button1) 

    { 

    savePreferences1("storedOnderwerp", editText1.getText().toString()); 

Toast.makeText(getApplicationContext(), 
     "Meldingonderwerp is aangepast.", Toast.LENGTH_LONG).show(); 
} 
} 
0

更改代碼這樣。這是優化處理您的按鈕方式點擊

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
switch(v.getId()) 
{ 
case R.id.button1: 
    savePreferences("storedName", editText.getText().toString()); 

    Toast.makeText(getApplicationContext(), 
      "Meldingtekst is aangepast.", Toast.LENGTH_LONG).show(); 
break; 
case R.id.button2: 
    savePreferences1("storedOnderwerp", editText1.getText().toString()); 

    Toast.makeText(getApplicationContext(), 
      "Meldingonderwerp is aangepast.", Toast.LENGTH_LONG).show(); 
break; 
} 
0

你不需要寫兩個的onClick(視圖v)的兩個按鈕,你只要有兩個把這個情況您的onClick(視圖v)內:

@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
switch(v.getId()){ 
case R.id.button: 
// code for button 1 
break; 
case R.id.button1: 
// code for button 2 
break; 
} 
} 
0

它總是傾向於根據Java爲Android中的clickLister創建內部類。

button1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
     savePreferences("storedName", editText.getText().toString()); 

    Toast.makeText(getApplicationContext(), 
      "Meldingtekst is aangepast.", Toast.LENGTH_LONG).show(); 
     } 
     }); 

    button2.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
     savePreferences("storedName", editText.getText().toString()); 

    Toast.makeText(getApplicationContext(), 
      "Meldingtekst is aangepast.", Toast.LENGTH_LONG).show(); 
     } 
     });