如何以编程方式隐藏按钮? [英] How to hide a button programmatically?

查看:85
本文介绍了如何以编程方式隐藏按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RelativeLayout,其中包含两个按钮.彼此重叠.

I have a RelativeLayout which contains two buttons. Which are overlapped on each other.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">


<Button android:text="Play"  
    android:id="@+id/play"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom = "true">
</Button>

<Button android:text="Stop "
    android:id="@+id/stop" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentBottom = "true">
</Button>


</RelativeLayout>

在单击事件被单击时,我想以编程方式仅显示一个按钮.

I want to programmatically show only one button at a time when its click event is called.

我尝试过:

playButton.setVisibility(1);

,但不起作用.以下是我要尝试的示例.

but it does not worked. Following is an example what I am trying to do.

playButton = (Button) findViewById(R.id.play);
playButton.setVisibility(1);
playButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //when play is clicked show stop button and hide play button

    }
});

推荐答案

您可以使用以下代码:

playButton = (Button) findViewById(R.id.play);
playButton.setVisibility(View.VISIBLE);
playButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //when play is clicked show stop button and hide play button
        playButton.setVisibility(View.GONE);
        stopButton.setVisibility(View.VISIBLE);
    }
});

这篇关于如何以编程方式隐藏按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆