求大神,求帮忙,求解答,先行谢谢!!
这是java中的代码
package com.example.sun;
import android.os.Bundle;
import android.app.Activity;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private ToggleButton toggleButton;
private ImageView imageView;
private MyThreah myThreah;
static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.util.Log.i("tag", "初始化开始");
toggleButton=(ToggleButton) findViewById(R.id.toggleButton1);
imageView=(ImageView) findViewById(R.id.imageView1);
myThreah=new MyThreah();
textView=(TextView) findViewById(R.id.TextView1);
android.util.Log.i("tag", "初始化结束");
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
android.util.Log.i("tag", "button被点击");
try {
imageView.setImageResource((arg1)?R.drawable.on:R.drawable.off);
myThreah.start();
} catch (Exception e) {
android.util.Log.i("tag", "button点击异常");
}
}
});
}
}
class MyThreah extends Thread{//自己定义的一个线程类
@Override
public void run() {
android.util.Log.i("tag", "run方法被执行");
MainActivity.textView.setText("改变的数"+"10");
android.util.Log.i("tag", "run完毕");
}
}
这是xml文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
android:id="@+id/toggleButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textOn="已开启"
android:textOff="已关闭"/>
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/off" />
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="这是文本框" />