界面采用TableLayout形式,第一行为TextView,动态显示文字;第二行显示三个鞋子图片;第三行显示按钮
tableview.xml文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background"
android:id="@+id/tableLayout1">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="2"
>
<TextView
android:text="@string/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
android:textSize="35dp"
android:textColor="#010d18"
android:id="@+id/textView1"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageview1"
android:src="@drawable/shoe_default"
android:paddingLeft="10dp"
android:layout_width="100dp"
android:layout_height="wrap_content">
</ImageView>
<ImageView
android:id="@+id/imageview2"
android:src="@drawable/shoe_default"
android:paddingLeft="10dp"
android:layout_width="100dp"
android:layout_height="wrap_content">
</ImageView>
<ImageView
android:id="@+id/imageview3"
android:src="@drawable/shoe_default"
android:paddingLeft="10dp"
android:layout_width="100dp"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
</TableRow>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal">
<Button
android:text="再玩一次"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</TableLayout>
2.MainActivity
int[] imageIds = new int[] {R.drawable.shoe_ok, R.drawable.shoe_sorry, R.drawable.shoe_default};
private ImageView image1;
private ImageView image2;
private ImageView image3;
private TextView result;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tableview);
image1 = (ImageView) findViewById(R.id.imageview1);
image2 = (ImageView) findViewById(R.id.imageview2);
image3 = (ImageView) findViewById(R.id.imageview3);
result = (TextView) findViewById(R.id.textView1);
button = (Button) findViewById(R.id.button1);
reset();
image1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isRight(v, 0);
}
});
image2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isRight(v, 1);
}
});
image3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isRight(v, 2);
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
reset();
result.setText(R.string.title);
image1.setImageAlpha(255);
image2.setImageAlpha(255);
image3.setImageAlpha(255);
image1.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
image2.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
image3.setImageDrawable(getResources().getDrawable(R.drawable.shoe_default));
}
});
}
private void reset() {
for (int i = 0; i < 3; i++) {
int temp = imageIds[i];
int index = (int) (Math.random() * 3);
imageIds[i] = imageIds[index];
imageIds[index] = temp;
}
}
private void isRight(View v, int index) {
image1.setImageDrawable(getResources().getDrawable(imageIds[0]));
image2.setImageDrawable(getResources().getDrawable(imageIds[1]));
image3.setImageDrawable(getResources().getDrawable(imageIds[2]));
image1.setImageAlpha(100);
image2.setImageAlpha(100);
image3.setImageAlpha(100);
ImageView v1 = (ImageView) v;
v1.setImageAlpha(255);
if (imageIds[index] == R.drawable.shoe_ok) {
result.setText("恭喜你, 猜对了,祝你幸福");
} else {
result.setText("很抱歉,猜错了,要不要再试一次?");
}
}