如何在 Android 中添加 webview 焦点?
此示例演示如何向 Android 中添加 webview 焦点。
步骤 1 − 在 Android Studio 中创建一个新项目,转至文件 ⇒ 新建项目,并填写所有必需的详细信息以创建一个新项目。
步骤 2 − 向 res/layout/activity_main.xml 添加以下代码。
<?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_width = "match_parent"
android:gravity = "center"
android:layout_height = "match_parent"
tools:context = ".MainActivity"
android:orientation = "vertical">
<WebView
android:id = "@+id/web_view"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
</LinearLayout>在上述代码中,我们使用 web 视图来显示 google.com。
步骤 3 − 向 src/MainActivity.java 添加以下代码
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web_view = findViewById(R.id.web_view);
web_view.loadUrl("https://www.google.com/");
web_view.requestFocus();
web_view.getSettings().setLightTouchEnabled(true);
}
}步骤 4 − 向 AndroidManifest.xml 添加以下代码
<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
package = "com.example.myapplication">
<uses-permission android:name = "android.permission.INTERNET"/>
<application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/AppTheme">
<activity android:name = ".MainActivity">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>我们尝试运行应用程序。我认为您已将实际的 Android 移动设备与计算机连接。要通过 Android Studio 运行应用,请打开其中一个项目的活动文件,然后单击工具栏中的运行
图标。选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备 –

单击 此处 下载项目代码
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP