“开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 1 天,点击查看活动详情”
如何进行页面的绘制?
1.把页面改成LinearLayout,也就是线性布局
2.添加一个按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:onClick="getHtml"
android:text="获取html"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
运行结果如下:
如何设置参数?
获取html的函数getHtml;
public void getHtml(View view) {
new Thread(){
public void run(){
String html=getHtml("http://www.baidu.com");
System.out.println(html);
}
}.start();
}
运行测试一下,这样就代表可以正常访问到一个网页了:
页面想要达到一个效果,就是把获取的html内容反馈到app的textview里面
我们运行一下测试,发现会报错:
大体意思就是哪一个线程创建它就由哪个线程来操作它,其他线程不可以操作。
那么如何解决上述的问题呢?
我们要自己写一个内部类MyHander:
public class MyHander extends Handler{
@Override
public void handleMessage(@NonNull Message msg) {
textView.setText(html);
}
}
然后我们需要声明一下MyHander:
下面我们运行测试一下:
发现可以成功获取啦!
“开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 1 天,点击查看活动详情”