一:布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools"
xmlns:app="schemas.android/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UrlActivity"
android:gravity="center">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开百度"/>
</LinearLayout>
二:调用
ample.demo;
import androidx.appcompat.app.AppCompatActivity;
t.Intent;
import android.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class UrlActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
Create(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_url);
btn = findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
try {
// 核心代码
Uri uri = Uri.parse("www.baidu");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
本文发布于:2025-03-30 13:46:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1743313572583324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |