我在android中做了一个简单的自定义浏览器,有非常基本的控件有刷新和返回,以及Url的edittext,我想做的是当用户ener任何url并点击“go”按钮时,它应该打开页面在同一浏览器中(不在默认浏览器中)。我已实现如下:但我的“go”按钮不起作用。它一直显示“找不到网页”。所以请帮助我这个我的代码如下:< / p>
subheader.java
package com.example.browserdemo;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SubHeader extends LinearLayout implements OnClickListener {
private Context context;
public ImageView btn_back;
public TextView txtTitle;
public ImageView btn_reload;
public EditText et_url;
public Button go;
public SubHeader(Context context) {
super(context);
this.context = context;
init();
}
public SubHeader(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public void init() {
View view = LayoutInflater.from(this.context).inflate(
R.layout.uc_sub_header, this, true);
this.btn_back = (ImageView) view.findViewById(R.id.btn_back);
this.btn_reload = (ImageView) view.findViewById(R.id.btn_reload);
this.go = (Button) view.findViewById(R.id.go);
this.et_url = (EditText) view.findViewById(R.id.et_url);
this.btn_back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_back) {
((Activity) this.context).finish();
}
}
}
main.java
package com.example.browserdemo;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
WebView Browser;
public SubHeader subHeader;
public EditText et_url;
public Button go;
public ImageView btn_back, btn_reload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Browser = (WebView) findViewById(R.id.webview);
this.subHeader = (SubHeader) findViewById(R.id.menu_header);
this.subHeader.btn_back.setVisibility(View.VISIBLE);
this.subHeader.btn_reload.setVisibility(View.VISIBLE);
btn_back = (ImageView) findViewById(R.id.btn_back);
btn_reload = (ImageView) findViewById(R.id.btn_reload);
Browser.loadUrl("http://www.google.com/");
et_url = (EditText) findViewById(R.id.et_url);
go = (Button) findViewById(R.id.go);
btn_reload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Browser.reload();
}
});
go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String theWebsite = et_url.getText().toString();
Browser.loadUrl(theWebsite);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(Browser.getWindowToken(), 0);
}
});
btn_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (Browser.canGoBack()) {
Browser.goBack();
}
}
});
generateUI();
}
public void generateUI() {
new GetRealTimeUrl().execute();
}
public class GetRealTimeUrl extends AsyncTask<Void, Void, Void> {
public ProgressDialog mProgress;
@Override
protected void onPreExecute() {
super.onPreExecute();
this.mProgress = new ProgressDialog(MainActivity.this);
this.mProgress.setMessage("Please Wait...");
this.mProgress.setCancelable(false);
this.mProgress.setIndeterminate(false);
this.mProgress.show();
}
@Override
protected Void doInBackground(Void... arg0) {
Browser.loadUrl("http://www.google.com/");
Browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Browser.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
Browser.getSettings().setLoadWithOverviewMode(true);
Browser.getSettings().setUseWideViewPort(true);
return null;
}
@Override
protected void onPostExecute(Void result) {
this.mProgress.hide();
this.mProgress.dismiss();
Message msg = new Message();
msg.arg1 = 1;
handler.sendMessage(msg);
super.onPostExecute(result);
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.arg1 == 1) {
}
}
};
}
答案 0 :(得分:0)
如评论所述,最好查看日志文件中的错误或logcat的内容
有一件事我注意到了我想要同时指出并覆盖了shouldOverrideUrlLoading ......这应该是在加载URL之前....
尝试放置Browser.loadUrl("http://www.google.com/");
在Browser.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
根据我的理解,程序流程首先设置侦听器/覆盖,以便它们准备好响应,然后加载要拦截的URL。
答案 1 :(得分:0)
确保 - 您是否在清单文件中添加了以下权限? -
<uses-permission android:name="android.permission.INTERNET" />