如何禁用点击按钮?

时间:2017-12-01 20:07:36

标签: java android

我有两个按钮,如果文件夹中没有文件,我想要禁用点击按钮运行时..但问题是按钮安装是否一直禁用..为什么?

        Button install = (Button) findViewById(R.id.install);
        final File file_1 = new File(Environment.getExternalStorageDirectory() + "/download/app-debug.apk");
        if(file_1.exists()){
            install.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+"app-debug.apk")),
                            "application/vnd.android.package-archive");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }

            });

        }else {

            install.setEnabled(false);

        }

    }

如果添加install.setEnabled(false);在onResume()按钮禁用所有时间,但如果从onResume()删除..工作良好:(

    @Override
    public void onResume() {
        super.onResume();
        final File file = new File(Environment.getExternalStorageDirectory() + "/download/app-debug.apk");
        final Button down = (Button) findViewById(R.id.down);
        down.setEnabled(!file.exists());
        final Button install = (Button) findViewById(R.id.install);
        install.setEnabled(false);
    }

1 个答案:

答案 0 :(得分:0)

你有:

onCreate方法:

    Button install = (Button) findViewById(R.id.install);
    install.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+"app-debug.apk")),
                        "application/vnd.android.package-archive");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

        });

onResume方法:

     @Override
        public void onResume() {
            super.onResume();
            final File file = new File(Environment.getExternalStorageDirectory() + "/download/app-debug.apk");
            final Button down = (Button) findViewById(R.id.down);
            down.setEnabled(!file.exists());
            final Button install = (Button) findViewById(R.id.install);
            install.setEnabled(file.exists());
        }
相关问题