Làm thế nào để ẩn một nút lập trình?


151

Tôi có một RelativeLayoutnút chứa hai nút. Mà chồng chéo lên nhau.

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

Tôi muốn lập trình chỉ hiển thị một nút tại một thời điểm khi sự kiện nhấp chuột của nó được gọi.

Tôi đã thử nó với:

playButton.setVisibility(1);

nhưng nó không hoạt động. Sau đây là một ví dụ những gì tôi đang cố gắng làm.

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

    }
});

Câu trả lời:


308

Bạn có thể sử dụng mã sau đây:

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
Cảm ơn sunil :) bạn có thể vui lòng cho biết sự khác biệt giữa View.VISIBLe và 1 (có phải chỉ là enum) không?
Vamsi Krishna B

1
Tại sao setVisibility thành 1? Đó không phải là bất kỳ giá trị không đổi.
pqsk

4
View.GONE làm cho mục không chiếm bất kỳ không gian bố trí. View.INVISIBLE dành chỗ cho mục này. Điều này thay đổi bố cục của chế độ xem khi bạn chuyển đổi mức độ hiển thị.
gb96

77

Hãy thử mã dưới đây -

playButton.setVisibility(View.INVISIBLE);

hoặc là -

playButton.setVisibility(View.GONE);

hiển thị lại với -

playButton.setVisibility(View.VISIBLE);

8

Vui lòng sử dụng dưới đây

View.GONE and View.VISIBLE

8

Hidde:

BUTTON.setVisibility(View.GONE);

Chỉ:

BUTTON.setVisibility(View.VISIBLE);

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


4

Tôi sẽ đề nghị bạn chỉ sử dụng một nút thay đổi văn bản và hành vi trên nút theo yêu cầu. Điều đó dễ dàng và sạch sẽ hơn là xử lý hai nút chồng chéo.

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

Tôi thích ý tưởng của bạn thực sự là những gì tôi làm trong iphone, bật một nút để làm nhiều việc. Nhưng tôi là người mới sử dụng Android, bạn có thể chỉ cho tôi một ví dụ về cách thực hiện việc này không ..
Rishi

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

Hãy thử điều này: playButton = (Button) findViewById(R.id.play); playButton.setVisibility(View.INVISIBLE);Tôi nghĩ rằng điều này sẽ làm điều đó.


1

Đối với "Xamarin Android":

FindViewById<Button>(Resource.Id.Button1).Visibility = ViewStates.Gone;

1

Mã Kotlin đơn giản hơn rất nhiều:

if(isVisable) {
    clearButton.visibility = View.INVISIBLE
}
else {
    clearButton.visibility = View.VISIBLE
}
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.