OnClickListener无法正常运行

时间:2014-10-08 06:19:39

标签: java android onclicklistener

我有一个按钮,应该将活动中的所有文本值设置为null。但是,OnClickListener似乎没有像我想象的那样正常运行。

问题是否与我在按钮后将文本设置为Prefs键有关?我已经在这个bug上呆了一段时间,似乎无法自己解决这个问题。

以下是代码:

package com.mikitz.rogsimple;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DamageChest extends Activity {

EditText chestdmg1, chestdmg2, chestdmg3, chestdmg4, chestdmg5, chestdmg6, chestdmg7, chestdmg8, chestdmg9, chestdmg10; 
Button heal;    

SharedPreferences pref;

String getChestDMG1, getChestDMG2, getChestDMG3, getChestDMG4, getChestDMG5, getChestDMG6, getChestDMG7, getChestDMG8, getChestDMG9, getChestDMG10; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.damage_chest);
    pref = getSharedPreferences("Character1", Context.MODE_PRIVATE);

    heal = (Button) findViewById(R.id.fullheal);

    chestdmg1 = (EditText) findViewById(R.id.dmg1);
    chestdmg2 = (EditText) findViewById(R.id.dmg2);
    chestdmg3 = (EditText) findViewById(R.id.dmg3);
    chestdmg4 = (EditText) findViewById(R.id.dmg4);
    chestdmg5 = (EditText) findViewById(R.id.dmg5);
    chestdmg6 = (EditText) findViewById(R.id.dmg6);
    chestdmg7 = (EditText) findViewById(R.id.dmg7);
    chestdmg8 = (EditText) findViewById(R.id.dmg8);
    chestdmg9 = (EditText) findViewById(R.id.dmg9);
    chestdmg10 = (EditText) findViewById(R.id.dmg10);

    getChestDMG1 = pref.getString("chestdmg1", "");
    getChestDMG2 = pref.getString("chestdmg2", "");
    getChestDMG3 = pref.getString("chestdmg3", "");
    getChestDMG4 = pref.getString("chestdmg4", "");
    getChestDMG5 = pref.getString("chestdmg5", "");
    getChestDMG6 = pref.getString("chestdmg6", "");
    getChestDMG7 = pref.getString("chestdmg7", "");
    getChestDMG8 = pref.getString("chestdmg8", "");
    getChestDMG9 = pref.getString("chestdmg9", "");
    getChestDMG10 = pref.getString("chestdmg10", "");

    final OnClickListener fullheal = new OnClickListener() {
            public void onClick(View v) {    
              SharedPreferences.Editor editor=pref.edit();
              editor.remove("chestdmg1");
              editor.remove("chestdmg2");
              editor.remove("chestdmg3");
              editor.remove("chestdmg4");
              editor.remove("chestdmg5");
              editor.remove("chestdmg6");
              editor.remove("chestdmg7");
              editor.remove("chestdmg8");
              editor.remove("chestdmg9");
              editor.remove("chestdmg10");
              editor.commit();                

              chestdmg1.setText("");
              chestdmg2.setText("");
              chestdmg3.setText("");
              chestdmg4.setText("");
              chestdmg5.setText("");
              chestdmg6.setText("");
              chestdmg7.setText("");
              chestdmg8.setText("");
              chestdmg9.setText("");
              chestdmg10.setText("");
            }
        };

    heal.setOnClickListener(fullheal); 

    chestdmg1.setText(getChestDMG1); 
    chestdmg2.setText(getChestDMG2); 
    chestdmg3.setText(getChestDMG3); 
    chestdmg4.setText(getChestDMG4); 
    chestdmg5.setText(getChestDMG5); 
    chestdmg6.setText(getChestDMG6); 
    chestdmg7.setText(getChestDMG7); 
    chestdmg8.setText(getChestDMG8); 
    chestdmg9.setText(getChestDMG9); 
    chestdmg10.setText(getChestDMG10); 



}

@Override
public void onBackPressed() 
{
    SharedPreferences.Editor editor=pref.edit();                    
        editor.putString("chestdmg1", chestdmg1.getText().toString());
        editor.putString("chestdmg2", chestdmg2.getText().toString());
        editor.putString("chestdmg3", chestdmg3.getText().toString());
        editor.putString("chestdmg4", chestdmg4.getText().toString());
        editor.putString("chestdmg5", chestdmg5.getText().toString());
        editor.putString("chestdmg6", chestdmg6.getText().toString());
        editor.putString("chestdmg7", chestdmg7.getText().toString());
        editor.putString("chestdmg8", chestdmg8.getText().toString());
        editor.putString("chestdmg9", chestdmg9.getText().toString());
        editor.putString("chestdmg10", chestdmg10.getText().toString());

        editor.commit();

        Toast.makeText(this, "GREAT SUCCESS!!!!", Toast.LENGTH_LONG).show();

        Intent intent = new Intent (this, DamageTracker.class);
        startActivity(intent); 
}

}

3 个答案:

答案 0 :(得分:2)

嗨,为什么不这样使用

heal.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences.Editor editor=pref.edit();
          editor.putString("chestdmg1", "");
          editor.putString("chestdmg2", "");
          editor.putString("chestdmg3", "");
          editor.putString("chestdmg4", "");
          editor.putString("chestdmg5", "");
          editor.putString("chestdmg6", "");
          editor.putString("chestdmg7", "");
          editor.putString("chestdmg8", "");
          editor.putString("chestdmg9", "");
          editor.putString("chestdmg10", "");
          editor.commit();                

        }
    });

它非常简单。如果你使用pref.edit(),它在模式更新或保存时看起来就像你一样。 让我们看看SharedPreferences

答案 1 :(得分:0)

将活动更改为

public class DamageChest extends Activity implements OnClickListener

然后将以下代码放入您的班级

heal.setOnClickListener(this);

覆盖onclick到类

@Override
public void onClick(View v) {
     switch (v.getId()) {
    case R.id.fullheal:
    // do your code here
    SharedPreferences.Editor editor=pref.edit();
          editor.remove("chestdmg1");
          editor.remove("chestdmg2");
          editor.remove("chestdmg3");
          editor.remove("chestdmg4");
          editor.remove("chestdmg5");
          editor.remove("chestdmg6");
          editor.remove("chestdmg7");
          editor.remove("chestdmg8");
          editor.remove("chestdmg9");
          editor.remove("chestdmg10");
          editor.commit();                

          chestdmg1.setText("");
          chestdmg2.setText("");
          chestdmg3.setText("");
          chestdmg4.setText("");
          chestdmg5.setText("");
          chestdmg6.setText("");
          chestdmg7.setText("");
          chestdmg8.setText("");
          chestdmg9.setText("");
          chestdmg10.setText("");
   break;
   default:

   break;
}

通过这种方式检查并告诉我它是否无效

答案 2 :(得分:0)

xml代码:

 <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClickbtn"
    android:text="Button2" />

在您的活动代码中:

public void onClickbtn(View v) {
    switch (v.getId()) {

    case R.id.button2:
        // put you code here
        break;

    default:
        break;
    }
   }