2013-11-27 36 views
1

我正在制作一个改变背景颜色的程序,例如,当我按白色时,将背景变为白色,黑色变成黑色等......并且在按下按钮之后其他人消失,并且我已经这样做了,但是我想要做的是每次点击Textview上的按钮都会出现/消失。我怎样才能做到这一点?如何点击Textview按钮出现/消失?

这是我的代码

LinearLayout myLayout; 
    LinearLayout myLayout2; 
    // Declare UI elements 
    private Button firstButton; 
    private Button secondButton, thirdButton; 
// private ImageView changeBackground; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     myLayout = (LinearLayout) findViewById(R.id.myLayout); 
     myLayout2 = (LinearLayout) findViewById(R.id.myLayout2); 

     // Initialize the UI components 



     firstButton = (Button) findViewById(R.id.button1); 
     // When we creating a button and if we expect that to use for event handling we have to set the listener 
     firstButton.setOnClickListener(this); 
     secondButton = (Button) findViewById(R.id.button2); 
     secondButton.setOnClickListener(this); 
     thirdButton = (Button) findViewById(R.id.button3); 
     thirdButton.setOnClickListener(this); 

    } 



@Override 
    public void onClick(View v) { // Parameter v stands for the view that was clicked. 

     switch(v.getId()) { 

     case R.id.button1: 
      myLayout.setBackgroundColor(Color.WHITE); 
      myLayout2.setVisibility(View.INVISIBLE); 
      break; 

     case R.id.button2: 
      myLayout.setBackgroundColor(Color.BLACK); 
      myLayout2.setVisibility(View.INVISIBLE); 
      break; 

     case R.id.button3: 
      myLayout.setBackgroundColor(Color.RED); 
      myLayout2.setVisibility(View.INVISIBLE); 
      break; 


     } 

    } 

的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#A9F5F2" 
    android:id="@+id/myLayout" 
    tools:context=".MainActivity" > 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:text="@string/hello_world" /> 

     <LinearLayout 
      android:id="@+id/myLayout2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#D8D8D8" 
      android:orientation="horizontal" 
      android:gravity="center" 
      tools:context=".MainActivity" > 

      <Button 
       android:id="@+id/button1" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"   
       android:text="@string/white" /> 



      <Button 
       android:id="@+id/button2" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/button1" 
       android:layout_alignBottom="@+id/button1" 
       android:layout_toRightOf="@+id/button1" 
       android:text="@string/black" /> 


      <Button 
       android:id="@+id/button3" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBaseline="@+id/button2" 
       android:layout_alignBottom="@+id/button2" 
       android:layout_toRightOf="@+id/button2" 
       android:text="@string/red" /> 
     </LinearLayout> 
</LinearLayout> 
+0

你的问题相当含糊。告诉我们发生了什么,并发布你的xml布局(或者如果你这样做了java布局代码)。另外,在每种情况下,myLayout2 ...行是否为复制粘贴错误或者是否有意? – RocketSpock

+0

我已添加完整的代码... – jaimito

回答

0

试试这个

myLayout.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v){ 
       myLayout2.setVisibility(View.VISIBLE); 
      } 
     }); 

并改变这一点:

myLayout2.setVisibility(View.INVISIBLE); 

到:

myLayout2.setVisibility(View.GONE); 

只是为了解释为什么你需要使用GONE而不是无形的,如果你设置的知名度,无形的对象将继续改变屏幕存在,你就不能看对象...而当您使用GONE时,该对象不存在于屏幕上,但存在于应用程序实例中。

+0

好的。我正在做的是如果我按下按钮这个按钮改变背景,例如button1 =白色,其他按钮消失后,我按下按钮1。但如果我点击屏幕,它应该再次出现按钮,以便能够选择另一个按钮。 – jaimito

+0

刚编辑我的答案 – GhostDerfel

+0

有效,但按钮不再出现 – jaimito

0

如果你希望你的按钮,他的所有特性消失使用:

myLayout2.setVisibility(View.GONE); 

,而不是

myLayout2.setVisibility(View.INVISIBLE); 

无形:

This view is invisible, but it still takes up space for layout purposes. 

GONE:

This view is invisible, and it doesn't take any space for layout purposes. 
+0

好的。但是接下来我想要做什么,如果我点击textview,它应该会再次出现按钮,以便能够按下另一个按钮。 – jaimito

+0

如果您希望按钮再次出现,请执行myLayout2.setVisibility(View.VISIBLE); – Pull