プログラムでボタンを非表示にする方法は?


151

私が持っているRelativeLayout2つのボタンが含まれています。互いに重なり合っている。

<?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>

クリックイベントが呼び出されたときに、プログラムで一度に1つのボタンのみを表示したいのですが。

私はそれを試してみました:

playButton.setVisibility(1);

しかし、それはうまくいきませんでした。以下は私がやろうとしている例です。

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

    }
});

回答:


308

次のコードを使用できます。

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

2
ありがとうsunil :) View.VISIBLeと1の違いを教えていただけますか?
Vamsi Krishna B 2012

1
なぜVisibilityを1に設定するのですか?それは定数値ではありません。
pqsk 2013

4
View.GONEは、アイテムがレイア​​ウトスペースを占有しないようにします。View.INVISIBLEは、アイテム用にスペースを予約します。これにより、表示を切り替えたときにビューのレイアウトが変更されます。
gb96

77

以下のコードを試してください-

playButton.setVisibility(View.INVISIBLE);

または-

playButton.setVisibility(View.GONE);

もう一度表示してください-

playButton.setVisibility(View.VISIBLE);



5
public void OnClick(View.v)
Button b1 = (Button) findViewById(R.id.playButton);
b1.setVisiblity(View.INVISIBLE);


4

1つのボタンのみを使用して、テキストとボタンの動作をオンデマンドで変更することをお勧めします。これは、重なっている2つのボタンを処理するよりも簡単でクリーンです。

@Override
public void onClick(View v) {
    String curText = ((TextView)v).getText();                 

    if(curText.equals("Play")){
        ((TextView)v).setText("Stop");
    }

    if(curText.equals("Stop")){
        ((TextView)v).setText("Play");
    }
 }

私、あなたのアイデアが実際に私はiphoneで複数の操作を行うための単一のボタンをトグルん私はアンドロイドに新しいですthings.Butのように、あなたがこれを行う方法の例に私を指すしてくださいすることができます。..
リシ

4
        Button button = (Button) findViewById(R.id.myButton);
        //set to visible
        button.setVisibility(View.VISIBLE);
        //set to invisble      
        button.setVisibility(View.INVISIBLE);
       //or
        button.setVisibility(View.GONE);


2

これを試してください:playButton = (Button) findViewById(R.id.play); playButton.setVisibility(View.INVISIBLE);これでうまくいくと思います。



1

Kotlinコードははるかに単純です。

if(isVisable) {
    clearButton.visibility = View.INVISIBLE
}
else {
    clearButton.visibility = View.VISIBLE
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.