android 鸡蛋在哪只鞋子小综合

界面采用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("很抱歉,猜错了,要不要再试一次?");
}

}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值