本篇博客主要讲解如何使用Intent来实现调动Android内置浏览器打开网页
本篇博客实现的是一个简单的输入网址打开网页的Demo

实现步骤:
  1. 实例化一个Intent对象
  2. 使用Uri.parse()方法解析网页地址
  3. intent.setAction()方法 PS:action规定了intent要完成的动作,是一个字符串常量,可以使系统自定义的action,比如本博客使用的ACTION_VIEW。
  4. intent.setData()方法,传入的是Uri,用于数据过滤,可以让系统自动自动去寻找匹配目标组件

界面设计展示

MainActivity的布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.ggq.intent_openweb.MainActivity"    android:weightSum="1">    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textAppearance="?android:attr/textAppearanceLarge"            android:text="网址:"            android:id="@+id/tv1" />        <EditText            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/et_web"            android:layout_weight="1" />    LinearLayout>    <LinearLayout        android:gravity="center"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="打开网页"            android:id="@+id/btn_oepn"            android:textSize="25dp" />    LinearLayout>LinearLayout>


Java代码实现

MainActivity代码:

package com.ggq.intent_openweb;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity {   private EditText et_web=null;   private Button btn_open=null;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn_open = (Button) findViewById(R.id.btn_oepn);        et_web = (EditText) findViewById(R.id.et_web);        btn_open.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                String address = et_web.getText().toString().trim();                Intent intent = new Intent();                Uri uri = Uri.parse(address);                intent.setAction(Intent.ACTION_VIEW);                intent.setData(uri);                MainActivity.this.startActivity(intent);            }        });    }}


最终效果

更多相关文章

  1. android支付宝客户端html5网页偶尔无法自动关闭问题
  2. scrollTo 以及 scrollBy方法使用说明
  3. Android(安卓)中 Retrofit 结合 RxJava使用
  4. linux和android端的pthread学习
  5. edittext 被软键盘挡住
  6. Robotium源代码编译
  7. Android(安卓)Studio适当修改
  8. Android(安卓)自定义Drawable 实现圆角圆形图片
  9. 我的第一个Android程序helloword及个人理解

随机推荐

  1. Android(安卓)设计原则【+整理】
  2. Android 如何在自定义界面上启用输入法 (
  3. java/android计算明天,今天,昨天,后天
  4. 为 Android* 设备构建动态 UI
  5. Android的多媒体框架OpenCore(PacketVide
  6. Android(安卓)Studio开发环境配置(无需上
  7. 《Android/OPhone开发完全讲义》连载(6):为T
  8. android列表【android开发记录片】androi
  9. android 使用自定义权限(1)
  10. Android静态变量的生命周期