intent实现网页跳转
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical"> <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="open net"/> </LinearLayout>
MinaActivity.java文件
package com.example.pprp.myapplication_lh; //使用内置intent打开网页 import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn01=(Button)findViewById(R.id.btn01); btn01.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Uri uri=Uri.parse("http://www.baidu.com"); Intent intent=new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); } }); } }
需注意:
findviewByid须在setContentView后面。