按钮问题日食

时间:2014-03-03 22:21:46

标签: android eclipse button count click

我想用一个按钮在eclipse中创建一个简单的应用程序。我想这样做,以便按下按钮10次后会弹出一条消息。问题是,当我启动应用程序并按下按钮10次时,没有任何反应。你能告诉我我做错了什么吗?

这是我的活动档案:

package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Game extends Activity implements android.view.View.OnClickListener{

    @Override 
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        Button gamebutton = (Button) findViewById(R.id.gamebutton);
    }
    @Override 
    public void onClick(View v){
        //TODO Auto-generated method stub
        int clicked = 0;
        clicked++;
        if(  clicked==10){
            AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
            gamebuild.setMessage("Good");
            gamebuild.setCancelable(false);
            gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Game.this.finish();

                }
            });
            gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
            dialog.cancel();        
                }
            });
        }

    }

}

感谢您的回复!我用这种方式编辑了活动文件:

package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class Game extends Activity implements android.view.View.OnClickListener{
    int clicked = 0;
    @Override 
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        Button gamebutton = (Button) findViewById(R.id.gamebutton);
    }

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

        clicked++;
        if(  clicked==10){
            AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
            gamebuild.setMessage("Good");
            gamebuild.setCancelable(false);
            gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Game.this.finish();

                }
            });
            gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
            dialog.cancel();        
                }
            });
        }

    }

}

仍然没有以正确的方式工作。抱歉愚蠢的问题:我是android的新手。

新编辑:

package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class Game extends Activity implements android.view.View.OnClickListener{
    int clicked = 0;
    @Override 
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        Button gamebutton = (Button) findViewById(R.id.gamebutton);
        gamebutton.setOnClickListener(this);

    }

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

        clicked++;
        if(  clicked==10){
            AlertDialog.Builder gamebuild = new AlertDialog.Builder(Game.this);
            gamebuild.setMessage("Good");
            gamebuild.setCancelable(false);
            gamebuild.setPositiveButton("Quit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Game.this.finish();

                }
            });
            gamebuild.setNegativeButton("One more!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
            dialog.cancel();        
                }
            });
        }

    }

}

2 个答案:

答案 0 :(得分:1)

您将计数器放在onClick函数中,导致它每次都重置。所以,移动

int clicked = 0;

从onClick内部到onCreate之前,例如:

int clicked = 0;
@Override
protected void onCreate....

这将使其设置为0一次,然后保持等于它的最后一个值,只要应用程序打开而不是被杀死。

答案 1 :(得分:0)

问题是在onClick中声明了变量 clicked 。所以它总是零。你必须在Game类中全局声明它。