OnCreate中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
changingTextView = (TextView) findViewById(R.id.changingTextView);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
}
脚本:
public void onClick(View v) {
if(v.getId() == R.id.button1){
PaintDrawable drawable = (PaintDrawable) button1.getBackground();
int color = drawable.getPaint().getColor();
if(color==Color.WHITE)
changingTextView.setText("continue");
else
changingTextView.setText("gameOVER");
}
if(v.getId() == R.id.button2){
PaintDrawable drawable = (PaintDrawable) button2.getBackground();
int color = drawable.getPaint().getColor();
if(color==Color.WHITE)
changingTextView.setText("continue");
else
changingTextView.setText("gameOVER");
}
if(v.getId() == R.id.button3){
PaintDrawable drawable = (PaintDrawable) button3.getBackground();
int color = drawable.getPaint().getColor();
if(color==Color.WHITE)
changingTextView.setText("continue");
else
changingTextView.setText("gameOVER");
}
here is the complete code+logcat image
main.xml中:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.bluetap.FirstActivity$PlaceholderFragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:minWidth="200dp"
android:text="Press The White Button"
android:textColor="@color/blue" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:background="@android:color/white"
android:minWidth="200dp"
android:text="Press The White Button"
android:textColor="@color/blue" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:minWidth="200dp"
android:text="Press The White Button"
android:textColor="@color/blue" />
<TextView
android:id="@+id/changingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_centerVertical="true"
android:text="text"
android:textSize="20sp" />
在Eclipse中,此代码中没有错误,但是当我运行应用程序时,您可以看到所有内容,一切都很好但是当您按下按钮1/2/3时,应用程序崩溃。 你能帮我找出原因吗? 我的应用程序只需要识别按钮背景是否为白色,如果是,它将继续,如果没有,它将暂停并说游戏结束。