java - 如何判断Button是否被点击

标签 java android

我必须检查按钮是否被单击。如果单击的应用程序必须执行一项任务,如果没有,应用程序必须执行另一项任务。我尝试这样做,但没有收到连接错误,我已将其放在 catch block 中的代码末尾。

 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;

这里我将 boolean 变量“Btnclicked”设置为 true。

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

                }
            });

但是执行完之后,光标直接到这里,显示“无连接”。

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

}

任何人都有解决方案。

最佳答案

正如你所说

"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:

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

最后,在按钮中执行您想要执行的任务:

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

关于java - 如何判断Button是否被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285204/

相关文章:

java - Spring MVC 端点在将空映射 {} 传递到 GET 请求时无法获取

android - 如何在 Android Studio (Gradle) 中添加 apache commons 集合

android - 模拟器控制android被禁用

android - addView() 之后的 "resultIndex is -1, the polygon must be invalid!"

java - eclipse 插件从 jar 使用的源代码创建类

java - 为什么不调用 Eclipse 插件处理程序?

android - 是否可以在 android gradle 中将 git 存储库声明为依赖项?

java - 更改 ListView 中 TextView 的文本

java - 找不到 DDMS 文件/opt/android-sdk-linux/platform-tools/hprof-conv ubuntu

java - 构造函数(Java)