问题
I have one Edit Text, 5 buttons and a Webview. Webview load url when user type url in edit text and tap Go button or hit Enter on soft keyboard. I have three problems here.
1-WebView loads website for example "www.google.com" but soft keyboard does not hide itself. I want to hide it like other browser do.
2-When user open a specific url and then try to remove it from Edit Text to open a different url, webview start loading that incomplete url and get focus automatically on every letter removing. For example user loads "www.google.com" then he try to remove url from end and as he remove "m", webview try to load "www.google.co" then edit text is out of focus and webview stole focus from it then user have to tap in edit text again to remove url but it tries to load after every letter removing. I need Webview to load url only when user tap on Go button or hit Enter on soft keyboard.
Update (forgot to add third problem, here it is)
3-Where can i check when WebView CanGoBack and CanGoForward because i want to set visibility of Back and Forward buttons to false at startup and enabled these two buttons while they CanGoBack and CanGoForward. Where should i put my piece of code to check these conditions?
I hope you guys can understand what i am trying to say. Sorry for my poor english.
Here is my xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="13"
tools:context="com.hbss.chatapp.rashidfaheem.hybridsoftwaresolutions.rashidfaheem.youseebrowser.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/etUrl"
android:layout_weight="3"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GO"
android:id="@+id/btnGo"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="4"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back"
android:id="@+id/btnBack"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Forward"
android:id="@+id/btnForward"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear"
android:id="@+id/btnClear"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reload"
android:id="@+id/btnReload"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wb"
/>
</LinearLayout>
</LinearLayout>
Here is my java code.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText etUrl;
Button btnGo, btnBack, btnForward, btnClear, btnReload;
WebView wb;
String url;
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wb = (WebView) findViewById(R.id.wb);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.setWebViewClient(new ourClient());
etUrl = (EditText) findViewById(R.id.etUrl);
btnGo = (Button) findViewById(R.id.btnGo);
btnBack = (Button) findViewById(R.id.btnBack);
btnForward = (Button) findViewById(R.id.btnForward);
btnClear = (Button) findViewById(R.id.btnClear);
btnReload = (Button) findViewById(R.id.btnReload);
btnGo.setOnClickListener(this);
btnReload.setOnClickListener(this);
btnBack.setOnClickListener(this);
btnForward.setOnClickListener(this);
btnClear.setOnClickListener(this);
etUrl.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction()==KeyEvent.ACTION_DOWN && i==KeyEvent.KEYCODE_ENTER) {
return true;
}
load();
return false;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnGo:
load();
break;
case R.id.btnBack:
if (wb.canGoBack())
wb.goBack();
break;
case R.id.btnForward:
if (wb.canGoForward())
wb.goForward();
break;
case R.id.btnReload:
wb.reload();
break;
case R.id.btnClear:
wb.clearHistory();
break;
}
}
public void load(){
url = etUrl.getText().toString();
if (!url.startsWith("http://")) {
url = "http://" + url;
}
try {
wb.loadUrl(url);
wb.requestFocus();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), " " + e, Toast.LENGTH_LONG).show();
}
}
}
回答1:
use this method to hide the keyboard:
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow()
.getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
call it in your load()
method like:
hideSoftKeyboard(MainActivity.this);
For your second question you need to call your method inside the condition like:
etUrl.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if ((keyEvent.getAction()==KeyEvent.ACTION_DOWN) && (i==KeyEvent.KEYCODE_ENTER)){
load(); //add it here
return true;
}
return false;
}
});
EDIT
For the webview
go back feature: override onKeyDown
in your activity and check condition like:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && wb.canGoBack()) {
wb.goBack();
//show your back button here
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
//show other buttons
return super.onKeyDown(keyCode, event);
}
You can add the check in your onResume()
and load()
also and show your button accordingly..
@Override
protected void onResume() {
super.onResume();
if(wb.canGoBack()){
//show back button}
else{
//other button
}
}
来源:https://stackoverflow.com/questions/43710772/webview-get-focus-automatically-when-removing-url-from-edit-text