解决方法:
https://www.jianshu.com/p/59aaf7b1c9e5
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hzx.androidthreadtest.MainActivity">
<Button
android:id="@+id/btn_change_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Text"/>
<TextView
android:id="@+id/show_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello world"
android:textSize="25sp"/>
</RelativeLayout>
Acitvity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.show_text);
//Button btn_change_text = (Button)findViewById(R.id.btn_change_text);
//btn_change_text.setOnClickListener((View.OnClickListener) MainActivity.this);
findViewById(R.id.btn_change_text).setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.btn_change_text:
new Thread(new Runnable(){
public void run(){
text.setText("Nice to meet you");
}
}).start();
break;
default:
break;
}
}
}
需要注意:
1、 需要实现implements View.OnClickListener
解决方法:Android中的异步消息处理