प्रोग्राम को एक बटन को छिपाने के लिए कैसे?


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
धन्यवाद सुनील :) क्या आप View.VISIBLe और 1 के बीच का अंतर बता सकते हैं (क्या यह सिर्फ एनम है)?
वम्सी कृष्णा B

1
क्यों 1 करने के लिए निर्धारितता? यह निरंतर मूल्यों में से कोई भी नहीं है।
pqsk

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

मेरा सुझाव है कि आप केवल एक बटन का उपयोग करें पाठ और मांग पर बटन पर व्यवहार को बदल सकते हैं। यह दो बटन को संभालने की तुलना में आसान और क्लीनर है जो अतिव्यापी हैं।

@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 में एक बटन से कई काम करने के लिए टॉगल करता हूं। लेकिन मैं एंड्रॉइड के लिए नया हूं, क्या आप कृपया मुझे इस पर एक उदाहरण दे सकते हैं कि यह कैसे करना है ..
ऋषि

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

कोटलिन कोड बहुत सरल है:

if(isVisable) {
    clearButton.visibility = View.INVISIBLE
}
else {
    clearButton.visibility = View.VISIBLE
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.