在基本的webview应用程序中,我希望用户能够在某种首选项菜单中手动更改链接。
如何将文本字段连接到mWebView.loadUrl?
<EditText
android:id="@+id/your_link"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
mWebView.loadUrl("http://domain.com/**your_link**/");
此外,我希望应用记住链接,以便用户不必每次都输入,只需在需要时进行更改。
答案 0 :(得分:2)
您可以在DateTime
上保存地址,在加载时检索该地址并在您打开SharedPreferences
或某些用户操作时加载WebView
的网址(按a例如Activity
:
<强> activity_main.xml中强>
Button
<强> WebViewActivity.java 强>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Change link"/>
</LinearLayout>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
答案 1 :(得分:2)
截图:
http://image.prntscr.com/image/c652033930e54938b170786a4f86570e.png http://image.prntscr.com/image/c7ef33aa812642b094f77cc71b2f3bcc.png
示例代码:
<强> DemoWebView.java 强>
public class DemoWebView extends AppCompatActivity {
private WebView webView;
private AutoCompleteTextView edtUrl;
public static final String PREFS_NAME = "HistoryPrefs";
public static final String PREFS_SEARCH_HISTORY = "SearchHistory";
private SharedPreferences sharedPreferences;
private Set<String> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webview);
edtUrl = (AutoCompleteTextView) findViewById(R.id.edt_url);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setWebViewClient(new MyBrowser(DemoWebView.this));
webView.canGoBack();
webView.canGoForward();
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setSupportZoom(false);
edtUrl.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_GO) {
webView.loadUrl("http://" + edtUrl.getText().toString());
addSearchInput(edtUrl.getText().toString());
}
return false;
}
});
sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
history = sharedPreferences.getStringSet(PREFS_SEARCH_HISTORY, new HashSet<String>());
setAutoCompleteSource();
}
private void setAutoCompleteSource() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, history.toArray(new String[history.size()]));
edtUrl.setAdapter(adapter);
}
private void addSearchInput(String input) {
if (!history.contains(input)) {
history.add(input);
setAutoCompleteSource();
}
}
private void savePrefs() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putStringSet(PREFS_SEARCH_HISTORY, history);
editor.commit();
}
@Override
protected void onStop() {
super.onStop();
savePrefs();
}
}
<强> webview.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/edt_url"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="8dp"
android:background="@android:drawable/editbox_background"
android:hint="Enter URL Here"
android:inputType="textPersonName"
android:maxLines="1"
android:imeOptions="actionGo"
android:textColor="@color/black_alpha_80"
android:textSize="16sp" />
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:clickable="true" />
</LinearLayout>
<强> MyBrowser.java 强>
public class MyBrowser extends WebViewClient {
private Activity mActivity;
private ProgressDialog mProgressDialog;
private String mainUrl = "";
public MyBrowser(Activity activity) {
mActivity = activity;
}
public MyBrowser(Activity activity, String mainUrl) {
this.mainUrl = mainUrl;
mActivity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
showProgress();
return true;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
view.loadUrl("");
Toast.makeText(mActivity, mActivity.getString(R.string.not_load_data), Toast.LENGTH_LONG);
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
@Override
public void onPageFinished(WebView view, String url) {
try {
Log.e("Progress==>", view.getProgress() + "");
dismissProgress();
} catch (Exception exception) {
Log.e("Exception==", exception + "");
exception.printStackTrace();
}
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
if (mProgressDialog == null) {
showProgress();
}
}
private void showProgress() {
Util.isCancelable = true;
mProgressDialog = Util.getProgressDialog(mActivity);
}
private void dismissProgress() {
Util.isCancelable = false;
Util.dismissDialog(mActivity, mProgressDialog);
}
}