我有一个编辑文本,5个按钮和一个Webview。当用户在编辑文本中键入url并点击Go按钮或在软键盘上按Enter键时,Webview加载URL。 我这里有三个问题。
1-WebView加载网站,例如“www.google.com”,但软键盘不会隐藏自己。我想像其他浏览器一样隐藏它。
2 - 当用户打开特定网址,然后尝试将其从“编辑文本”中删除以打开其他网址时,webview会开始加载该不完整的网址,并自动获取每封删除的信息。例如,用户加载“www.google.com”,然后他尝试从末尾删除网址,当他删除“m”时,webview尝试加载“www.google.co”,然后编辑文本失焦,而webview则从中获取焦点然后用户必须再次点击编辑文本以删除网址,但它会尝试在每个字母删除后加载。只有当用户点击Go按钮或按下软键盘上的Enter键时,我才需要Webview加载URL。
更新(忘了添加第三个问题,就在这里)
3 - 我可以在哪里检查WebView CanGoBack和CanGoForward,因为我想在启动时将后退和前进按钮的可见性设置为false,并在CanGoBack和CanGoForward时启用这两个按钮。我应该把我的代码放在哪里来检查这些条件?
我希望你们能理解我想说的话。抱歉我的英语不好。
这是我的xml代码。
<?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>
这是我的java代码。
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();
}
}
}
答案 0 :(得分:2)
使用此方法隐藏键盘:
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);
}
在您的load()
方法中调用它,如:
hideSoftKeyboard(MainActivity.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)){
load(); //add it here
return true;
}
return false;
}
});
修改强>
对于webview
返回功能:在您的活动中覆盖onKeyDown
并检查条件如下:
@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);
}
您也可以在onResume()
和load()
添加支票,并相应地显示您的按钮。
@Override
protected void onResume() {
super.onResume();
if(wb.canGoBack()){
//show back button}
else{
//other button
}
}