Я делаю программу, которая меняет цвет фона, например, когда я нажимаю белый, меняю фон на белый, черный на черный и т. Д. ... и после нажатия кнопки остальные исчезают, а я уже сделали это, но то, что я хочу сделать, - это то, что каждый щелчок на 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>
Ваш вопрос довольно расплывчатый. Расскажите нам, что происходит, и разместите свой xml-макет (или код макета java, если вы это сделали). Кроме того, является ли строка myLayout2 ... в каждом случае ошибкой копирования или вставки? – RocketSpock
У меня есть весь мой код ... – jaimito