两个OnClickListeners,两个按钮。 (savedpreferences)

时间:2013-10-29 11:12:06

标签: java android

我有两个edittext块+两个按钮(使用保存的首选项),我似乎无法弄清楚如何在两者上使用onclicklistener?我现在有两个按钮需要onclicklistener上的其他东西。如何更改代码以使其工作?

我对android很新。

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();
}

7 个答案:

答案 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();
    }
}

答案 1 :(得分: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);


            }
        });

答案 2 :(得分:0)

您只需要覆盖onClick()方法一次。写得像这样:

@Override

public void 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();
 }
 }

答案 3 :(得分: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;
}

答案 4 :(得分:0)

你不需要为两个按钮写两个onClick(View v),你只需要两个将这个条件放在你的onClick(View 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;
}
}

答案 5 :(得分: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();
        }
       });

答案 6 :(得分:0)

我认为您需要了解接口的概念才能理解您应该做什么。您可以查看此链接,以帮助您了解如何使用onClickListner。

http://martin.cubeactive.com/android-onclicklitener-tutorial/