如何在 Android webview 中保存密码?
本例演示如何在 Android webview 中保存密码。
步骤 1 − 在 Android Studio 中新建一个项目,依次点击 File ⇒ New Project 并填写所有必需详细信息以新建一个项目。
步骤 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 视图来展示 facebook.com。
步骤 3 − 添加以下代码至 src/MainActivity.java
package com.example.myapplication; import android.app.ProgressDialog; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; 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); final ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("Loading Data..."); progressDialog.setCancelable(false); WebView web_view = findViewById(R.id.web_view); web_view.requestFocus(); web_view.getSettings().setJavaScriptEnabled(true); web_view.getSettings().setSavePassword(true); web_view.loadUrl("https://touch.facebook.com/"); web_view.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); web_view.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { if (progress < 100) { progressDialog.show(); } if (progress = = 100) { progressDialog.dismiss(); } } }); } }
步骤 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 运行该应用程序,请打开你其中一个项目的活动文件并单击工具栏中的运行 图标。选择你的移动设备作为选项,然后检查将会显示默认屏幕的移动设备:
单击此处下载项目代码
广告