如何设置按钮隐形android

时间:2012-04-03 10:03:31

标签: android button invisible

所以我试着让A按钮看不见,我首先在论坛上搜索答案并尝试了它,但它只是不起作用。

int level=0;
    try{
        String FILENAME = "TowerFile";
        FileInputStream fos = openFileInput(FILENAME);
        byte[] b = new byte[1];
        fos.read(b);
        fos.close();
        if(new String(b).equalsIgnoreCase("1")){
            level=1;
        }
        if(new String(b).equalsIgnoreCase("2")){
            level=2;
        }
        if(new String(b).equalsIgnoreCase("3")){
            level=3;
        }
        if(new String(b).equalsIgnoreCase("4")){
            level=4;
        }
        if(new String(b).equalsIgnoreCase("5")){
            level=5;
        }
    }catch (java.io.FileNotFoundException e) {
    } catch (IOException e) {
            e.printStackTrace();
    }

    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);
    Button button4 = (Button) findViewById(R.id.button4);
    Button button5 = (Button) findViewById(R.id.button5);



    if(level==1){
        button1.setVisibility(View.INVISIBLE);
    }

所以我首先从我的文件中获取一个变量,然后我想根据输出使A按钮不可见。我从我的档案中得到了怀特号码,但它没有做任何事情,我也做了0但没有任何作品

4 个答案:

答案 0 :(得分:4)

尝试这样做

button1.setVisibility(Button.INVISIBLE);

答案 1 :(得分:4)

buttonName.setVisibility(View.GONE);

这比(View.INVISIBLE)更好,因为按钮没有从布局中获得任何空间。如果你想让按钮再次可见,只需使用:

buttonName.setVisibility(View.VISIBLE);

答案 2 :(得分:1)

为使按钮不可见而编写的代码是正确的。检查变量级别是否 1

答案 3 :(得分:0)

以下代码在主/ UI线程上运行以隐藏按钮。如果您希望按钮是透明的,请将代码View.GONE更改为View.INVISIBLE。

家是上下文。

try {

runOnUiThread(new Runnable() {

    @Override
    public void run() {                         
    ((Button) findViewById(R.id.my_button)).setVisibility(View.GONE);
    }
});
    } catch (Exception e) {

    Toast.makeText(Home.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}

如果你喜欢这个答案,请投票。感谢