简体   繁体   English

如何检查按钮是否被单击

[英]how to check whether Button is clicked or not

I have to check whether the button is clicked or not.if clicked application has to do one task and if not, application has to do another task. 我必须检查按钮是否被单击。如果单击的应用程序必须执行一项任务,否则,应用程序必须执行另一项任务。 I tried to do this, but I am getting no connection error which I have put at the end of the code in catch block. 我尝试这样做,但没有收到连接错误,该错误已在catch块的代码末尾添加。

 protected void onCreate(Bundle savedInstanceState) {
    PracticeVO practiceObj;

    try {

        setTitle("Klarity");
        setPrefBtn = (Button) findViewById(R.id.setPrefBtn);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_klarity_home);

        /*
         * asynchronous calls
         */

        StrictMode.ThreadPolicy policy = new     StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        final ConnectionHelper con = new ConnectionHelper();
        /*
         * It will connect to DB and fetches the Practice Information
         */
        if (Btnclicked == false) {

            String allPracticesStr = null;

here I have set the boolean variable 'Btnclicked' true. 在这里,我将布尔变量'Btnclicked'设置为true。

 setPrefBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Btnclicked=true;
                    Intent setPrefIntent = new Intent(KlarityHome.this,
                            SetPreferences.class);
                    startActivity(setPrefIntent);

                }
            });

But After executing this The cursor is directly goin here and displaying 'no connection'. 但是执行完之后,光标将直接进入此处并显示“ no connection”。

catch (Exception ex) {
        Context context = getApplicationContext();
        CharSequence text = "There is some error in application";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        System.out.println("no connection");
    }

}

Anyone has solution on this. 任何人都对此有解决方案。

as you said 如你所说

"the button is clicked or not.if clicked application has to do one task and if noapplication has to do another task" so, if i were you, i'll put 2 radio buttons, every one with the text of the task you want to do, and add a listener for them, like this: “按钮是否被单击。如果单击的应用程序必须执行一项任务,如果没有应用程序必须执行另一项任务”,那么,如果我是您,我将放置2个单选按钮,每个单选按钮均包含所需任务的文本为此,并为其添加一个侦听器,如下所示:

radiobutton1.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            radiobutton2.setChecked(false);
        }
    });

radiobutton2.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                radiobutton1.setChecked(false);
            }
    });

and last, in the button do the task you want to do: 最后,在按钮中执行您要执行的任务:

    Button.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
    if(radiobutton1.isChecked){
    //do task 1
}else{
//do task2}
    }
    });

You are getting an NPE(NullPointerException) since you havent initialised Btnclicked in your code at the time of usage.ie you are checking whether Btnclicked is false like 您正在使用NPE(NullPointerException)因为您在使用时Btnclicked在代码中初始化Btnclicked 。即,您正在检查Btnclicked是否为false,例如

if (Btnclicked == false) {

But at the time of this checking, the value of Btnclicked is null and hence an NPE. 但是Btnclicked此检查时, Btnclicked值为null ,因此为NPE。

So to get rid of this, you just add either 因此,要摆脱这种情况,您只需添加

Btnclicked=true;

or 要么

Btnclicked=false;

before the if loop according to your logic. 根据您的逻辑在if循环之前。

OR 要么

or just replace the declaration from Boolean Btnclicked; 或者只是替换Boolean Btnclicked;的声明Boolean Btnclicked; to Boolean Btnclicked=false; Boolean Btnclicked=false;

Hope this helps. 希望这可以帮助。

Remove Btnclicked you don't need that. 删除您不需要的Btnclicked。 OnClickListner is meant for what you're trying to do. OnClickListner适用于您要执行的操作。 The code which you want to run when button is not pressed put it before 未按下按钮时要运行的代码放在前面

//When button is not pressed
setPrefBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //When button is pressed
                }
            });

And the code which you want to run when the button is clicked place it inside the onClick(){} 并将您希望在单击按钮时运行的代码放在onClick(){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM