我正在制作一个改变背景颜色的程序,例如,当我按下白色,将背景改为白色,将黑色改为黑色等等......按下按钮后,其他按钮消失,我已经这样做但我想要做的是每次点击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 :(得分: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.
答案 1 :(得分:0)
试试这个
myLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
myLayout2.setVisibility(View.VISIBLE);
}
});
并改变这一点:
myLayout2.setVisibility(View.INVISIBLE);
为:
myLayout2.setVisibility(View.GONE);
只是为了解释为什么你需要使用GONE而不是INVISIBLE,如果你将可见性设置为INVISIBLE,对象将继续改变为存在于屏幕中,你只是看不到对象......当你使用时GONE对象在屏幕上不存在但存在于app实例中。