Androidスタジオでボタンに取り組んでいますが、クリックするたびにボタンの背景が変わり、そうでないときは別の背景があります。やり方がわかりません、教えていただけませんか?
3985 次
2 に答える
2
XML レイアウト:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change"
android:onClick="changeBack" />
</LinearLayout>
このコードをアクティビティに追加します
public boolean firstImage = true;
public void changeBack(View view)
{
if (firstImage)
((LinearLayout)findViewById(R.id.main)).setBackgroundResource(R.drawable.secondimage);
else
((LinearLayout)findViewById(R.id.main)).setBackgroundResource(R.drawable.firstimage);
firstImage = !firstImage;
}
于 2013-09-28T18:08:52.200 に答える