วิธีซ่อนปุ่มโดยทางโปรแกรม


152

ฉันมีRelativeLayoutปุ่มที่มีสองปุ่ม ซึ่งจะซ้อนทับกัน

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

ฉันต้องการแสดงรายการทางปุ่มครั้งละหนึ่งรายการเท่านั้นเมื่อมีการเรียกกิจกรรมการคลิก

ฉันลองด้วย:

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 (มันเป็นแค่ enum) ได้ไหม?
Vamsi Krishna B

1
ทำไม setVisibility ถึง 1 นั่นไม่ใช่ค่าคงที่ใด ๆ
pqsk

4
มุมมอง GONE ทำให้รายการไม่ใช้พื้นที่ในการจัดวาง View.INVISIBLE จองพื้นที่สำหรับรายการ สิ่งนี้จะเปลี่ยนเค้าโครงของมุมมองเมื่อคุณสลับการมองเห็น
gb96

77

ลองรหัสด้านล่าง -

playButton.setVisibility(View.INVISIBLE);

หรือ -

playButton.setVisibility(View.GONE);

แสดงอีกครั้งด้วย -

playButton.setVisibility(View.VISIBLE);





4

ฉันอยากจะแนะนำให้คุณใช้เพียงปุ่มเดียวในการเปลี่ยนข้อความและพฤติกรรมบนปุ่มตามความต้องการ ง่ายกว่าและสะอาดกว่าการจัดการสองปุ่มที่ซ้อนทับกัน

@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 toggling ปุ่มเดียวเพื่อทำหลายสิ่ง แต่ฉันใหม่กับ android คุณช่วยชี้ฉันเป็นตัวอย่างในการทำสิ่งนี้ได้ไหม ..
ฤษี

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);ฉันคิดว่าสิ่งนี้จะทำได้



โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.