80,350
社区成员
发帖
与我相关
我的任务
分享
package m.m.m;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Message msg = mHandler.obtainMessage();
msg.what = 1001;
mHandler.sendMessageDelayed(msg, 1000);
}
int k=0;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1001:
/*
* 这里是把button1的位置发生改变
* */
Button bt1=(Button)findViewById(R.id.button1);
bt1.layout(20, 20, 50, 50);
Message mg1 = mHandler.obtainMessage();
mg1.what = 1002;
mHandler.sendMessageDelayed(mg1, 1000);
break;
case 1002:
/*
* 这里是把button2的内容发生改变
* */
Button bt2=(Button)findViewById(R.id.button2);
bt2.setText(" "+k);
k++;
Message mg2 = mHandler.obtainMessage();
mg2.what = 1001;
mHandler.sendMessageDelayed(mg2, 1000);
break;
}
super.handleMessage(msg);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ly"
>
<Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_x="26dip" android:layout_y="121dip"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:layout_x="230dip" android:layout_y="121dip"></Button>
</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ly"
>
<Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_x="26dip" android:layout_y="121dip"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:layout_x="230dip" android:layout_y="121dip"></Button>
</FrameLayout>