在Activiey 中更改 并刷新Layout(布局)中的控

系统 1627 0

/*

里面有自定义Toast的显示位置

*/

//DoorActivity

package temp.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DoorActivity extends Activity {

// private LayoutInflater inflater = null;

private ImageView doorImageView;
//当前操作的门
private int thisWindow=1;
private Button okDoorButton;
private Button openDoorButton;
private Button closeDoorButton;
private Toast toast;
private DoorOperationView doorOperationView;
private LinearLayout linearLayoutDoor;

@Override
public void onCreate(Bundle savedInstanceState) {
//inflater = LayoutInflater.from(this);
super.onCreate(savedInstanceState);

//Thread.currentThread().start();
setContentView(R.layout.doorview);

linearLayoutDoor = (LinearLayout) findViewById(R.id.linearLayoutDoor);
// 获取门控件
doorImageView = (ImageView) findViewById(R.id.doorImageView);
//获取当前操作的门
getThisDoor();
// 设置门开始界面
setDoorFace(); // dooropen 打开;doopclose 为关闭

doorOperationView =new DoorOperationView(DoorActivity.this,Values.doorIsOpen[thisWindow]);

openDoorButton = (Button) DoorActivity.this.findViewById(R.id.openDoorButton);

openDoorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("监听到将要 打开门");
if (!Values.doorIsOpen[thisWindow]) {
// 更新门状态
updateDoorState(true);
linearLayoutDoor.removeAllViews();
linearLayoutDoor.addView(doorOperationView);
}else {
toast = Toast.makeText(getApplicationContext(),"门已经打开了!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(400);
toast.show();
}
}

});

closeDoorButton = (Button) DoorActivity.this.findViewById(R.id.closeDoorButton);
closeDoorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("监听到将要 关闭门");
if (Values.doorIsOpen[thisWindow]) {
// 更新门状态
updateDoorState(false);
linearLayoutDoor.removeAllViews();
linearLayoutDoor.addView(doorOperationView);
}else {
toast = Toast.makeText(getApplicationContext(),
"门已经关上了!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(400);
toast.show();
}
}
});

/* okDoorButton = (Button) DoorActivity.this.findViewById(R.id.okDoorButton);
okDoorButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
System.out.println("监听到确定按钮");
if (doorOperationView != null) {
finish();
}
}
});*/

}
public void updateDoorState(boolean blean) {
Values.doorIsOpen[thisWindow]=blean;
doorOperationView =new DoorOperationView(DoorActivity.this,Values.doorIsOpen[thisWindow]);
}

//获取当前操作的门
public int getThisDoor(){
thisWindow=Values.thisWindow-1;
System.out.println("This Door is "+thisWindow);
if(Values.doorIsOpen[thisWindow])
System.out.println("门已打开");
else {
System.out.println("门已关闭");
};
return thisWindow;
}
// 设置门开始界面
public void setDoorFace(){
if(Values.doorIsOpen[thisWindow])
doorImageView.setImageResource(R.drawable.dooropen);
else {
doorImageView.setImageResource(R.drawable.doorclose);
}
}

}

//DoorOperationView

package temp.com;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

public class DoorOperationView extends View{

Context mContext = null;
Bitmap bitmap=null;
/* 定义一个Drawable对象 */
Drawable mBitDrawable = null;

boolean isOpen=false;

private Paint paintNormal = null;// 小画笔

public DoorOperationView(DoorActivity doorActivity,boolean isOpen)
{
super(doorActivity);
this.isOpen=isOpen;
paintNormal = new Paint();
paintNormal.setAntiAlias(true);
paintNormal.setStyle(Paint.Style.STROKE);
paintNormal.setColor(Color.BLACK);
paintNormal.setStrokeWidth(1);

if(isOpen){//窗户为打开状态 这里是关闭动画
bitmap = ((BitmapDrawable)doorActivity.getResources().getDrawable(R.drawable.dooropen)).getBitmap();
}else if (!isOpen) {//窗户为关闭状态 这里是打开动画
bitmap = ((BitmapDrawable)doorActivity.getResources().getDrawable(R.drawable.doorclose)).getBitmap();
}
}

public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawBitmap(bitmap, 0, 0, paintNormal);
}
}

//Values

package temp.com;

public class Values {
public static int thisWindow=1;//当前操作的控件的ID

//对应七扇门,是否打开 true 表示门是打开的 false 表示门是关闭的
public static boolean doorIsOpen[]={
true, //门1
true, //门2
true, //门3
true, //门4
true, //门5
true, //门6
true, //门7
};


}

//点击打开按钮时

在Activiey 中更改 并刷新Layout(布局)中的控件(ImageView)的显示

//点击关闭按钮时

在Activiey 中更改 并刷新Layout(布局)中的控件(ImageView)的显示

//源码下载

http://download.csdn.net/source/3222583

如果没有资源分的或没有CSDN号的可以留邮箱,或发邮件至254443233@qq.com;本人免费放送

在Activiey 中更改 并刷新Layout(布局)中的控件(ImageView)的显示


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论