为什么80%的码农都做不了架构师?>>>
不知道怎么写。
1.底部菜单
2.返回和刷新的按钮
3.在fargment中的返回
还好把代码写出来吧。
这个是第一个微信页面代码
public class Mainfragment0 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_mainfragment0, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final WebView tv_show = (WebView) view.findViewById(R.id.tv_show);
tv_show.loadUrl("https://www.hao123.com/");
tv_show.getSettings().getJavascriptEnabled();//Javascript调用
tv_show.getSettings().setSupportZoom(true); //设置可以支持缩放
tv_show.getSettings().setBuiltInZoomControls(true);//设置是否出现缩放工具
tv_show.getSettings().setUseWideViewPort(true);//扩大缩放的比例
//自适应屏幕
tv_show.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
tv_show.getSettings().setLoadWithOverviewMode(true);
tv_show.setWebViewClient(new WebViewClient(){//继续在当前网页打开
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//使网页后退
ImageButton back = (ImageButton) view.findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_show.goBack();
}
});
//刷新网页
ImageButton refresh = (ImageButton) view.findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_show.reload();
}
});
//点击桌面按钮,进行返回,而不是直接结束finish页面
tv_show.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if (tv_show != null && tv_show.canGoBack()) {
tv_show.goBack();
}else {
fragmentActivity.popBackStack();
}
return true;
}
return false;
}
});
}
}
还有一个fragment的代码
public class fragmentActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
//Fragment Object
private Mainfragment0 mainfragment0;
private Mainfragment1 mainfragment1;
private Mainfragment2 mainfragment2;
private Mainfragment3 mainfragment3;
private FragmentManager fragmentManager;//fragment的集合处理器
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainfragment);
fragmentManager = getFragmentManager();
nitLayout();
}
//获取RadioGroup,RadioButton的事件,并且设置图片的大小,设置状态
private void nitLayout() {
RadioGroup rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
RadioButton rb_weixin = (RadioButton) findViewById(R.id.rb_weixin);
RadioButton rb_tongxunlu = (RadioButton) findViewById(R.id.rb_tongxunlu);
RadioButton rb_faxian = (RadioButton) findViewById(R.id.rb_faxian);
RadioButton rb_wo = (RadioButton) findViewById(R.id.rb_wo);
//获取RadioGroup的点击事件
rg_tab_bar.setOnCheckedChangeListener(this);
//获取第一个单选按钮,并设置其为选中状态
rb_weixin.setChecked(true);
//定义底部标签图片的大小
Drawable drawableweixin = getResources().getDrawable(R.drawable.tab_menu_weixin);
drawableweixin.setBounds(0, 0, 100, 80);
rb_weixin.setCompoundDrawables(null, drawableweixin, null, null);
Drawable drawabletongxunlu = getResources().getDrawable(R.drawable.tab_menu_tongxunlu);
drawabletongxunlu.setBounds(0,0,100,80);
rb_tongxunlu.setCompoundDrawables(null,drawabletongxunlu,null,null);
Drawable drawablefaxian = getResources().getDrawable(R.drawable.tab_menu_faxian);
drawablefaxian.setBounds(0,0,100,80);
rb_faxian.setCompoundDrawables(null,drawablefaxian,null,null);
Drawable drawablewo = getResources().getDrawable(R.drawable.tab_menu_wo);
drawablewo.setBounds(0,0,100,80);
rb_wo.setCompoundDrawables(null,drawablewo,null,null);
}
//获取底部菜单点击事件
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
hideAllFragment(fragmentTransaction);
switch (checkedId) {
case R.id.rb_weixin:
if (mainfragment0 == null) {
mainfragment0 = new Mainfragment0();
fragmentTransaction.add(R.id.frame, mainfragment0);
} else {
fragmentTransaction.show(mainfragment0);
}
break;
case R.id.rb_tongxunlu:
if (mainfragment1 == null) {
mainfragment1 = new Mainfragment1();
fragmentTransaction.add(R.id.frame, mainfragment1);
} else {
fragmentTransaction.show(mainfragment1);
}
break;
case R.id.rb_faxian:
if (mainfragment2 == null) {
mainfragment2 = new Mainfragment2();
fragmentTransaction.add(R.id.frame, mainfragment2);
} else {
fragmentTransaction.show(mainfragment2);
}
break;
case R.id.rb_wo:
if (mainfragment3 == null) {
mainfragment3 = new Mainfragment3();
fragmentTransaction.add(R.id.frame, mainfragment3);
} else {
fragmentTransaction.show(mainfragment3);
}
break;
}
fragmentTransaction.commit();
}
//隐藏所有的Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction) {
if (mainfragment0 != null) {
fragmentTransaction.hide(mainfragment0);
}
if (mainfragment1 != null) {
fragmentTransaction.hide(mainfragment1);
}
if (mainfragment2 != null) {
fragmentTransaction.hide(mainfragment2);
}
if (mainfragment3 != null) {
fragmentTransaction.hide(mainfragment3);
}
}
public static void popBackStack() {
}
}
MainActivity的内容,主要可以输入网址,点击进入后。即可打开网址
里面还有个判断http是否要添加
public class MainActivity extends Activity {
private EditText url;
private WebView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button run = (Button) findViewById(R.id.enter);
url = (EditText) findViewById(R.id.url1);
show = (WebView) findViewById(R.id.show1);
// 点击进入按钮后,调用go方法
run.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
go();
}
});
}
@SuppressLint("SetJavascriptEnabled")
private void go() {
// 获取网址
String urlcontent = url.getText().toString();
//判断网址是否存在http
String URL = "http";
if (!URL.equals(urlcontent.substring(0, 4))) {
urlcontent = URL + "://" + urlcontent;
}
System.out.println(urlcontent.substring(0, 4));
System.out.println(urlcontent);
show.loadUrl(urlcontent);
show.getSettings().setJavascriptEnabled(true);//读取使用网页的Javascript
show.getSettings().setSupportZoom(true); //设置可以支持缩放
show.getSettings().setBuiltInZoomControls(true);//设置是否出现缩放工具
show.getSettings().setUseWideViewPort(true);//扩大缩放的比例
//点击页面的链接,不是新开的Android自带的浏览器,而是覆盖webview
show.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
show.loadUrl(url);
return true;
}
});
}
//浏览网页的时候,点击系统“BACK”,整个browser结束自身,所以要在Activity中处理并消费掉BACK事件
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KEYCODE_BACK) && show.canGoBack()) {
show.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
顶部返回按钮的代码
// 按钮返回
Button btnBack = (Button) findViewById(R.id.bar_title_backButton);
btnBack.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if (webview.canGoBack()) {
webview.goBack();
}
else{
finish();
}
}
});
WebViewClient方法
1. shouldOverrideUrlLoading(WebView view, String url)
官方注释:Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided,by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".
翻译:当一个新的url要在当前WebView进行加载的时候,这个方法给应用一个机会来控制url的处理。如果WebView没有setWebViewClient,则默认操作是WebView将询问Activity Manager获取合适的handler处理url。如果WebView设置了setWebViewClient,返回true代表当前应用来处理url,返回false则代表当前webview来处理url。如果http请求是POST方法,该方法将不会被调用。
代码示例:
/**
* 所有以www.example.com开头的url调用系统浏览器打开 其他的url在当前webview打开
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("http://www.example.com") != -1) {
// 调用系统默认浏览器处理url
view.stopLoading();
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
return false;
}
2. shouleOverrideKeyEvent(WebView view, KeyEvent event)
官方注释:Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key events need to be filtered this way. If return true, WebView will not handle the key event. If return false, WebView will always handle the key event, so none of the super in the view chain will see the key event. The default behavior returns false.
翻译:给当前应用一个机会来异步处理按键事件。返回true,WebView将不会处理该按键事件,返回false,WebView将处理该按键事件。默认返回是false。
3. onPageStarted(WebView view, String url, Bitmap favicon)和onPageFinished(WebView view, String url)
官方注释:Notify the host application that a page has started loading. This method is called once for each main frame load so a page with iframes or framesets will call onPageStarted one time for the main frame. This also means that onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe.
翻译:当页面开始加载时被调用。但是,当页面被嵌套时(例如iframe里有一个链接跳转),该方法将不会被调用。(今天就遇到了这种情况,可以通过重载onLoadResource来控制url跳转)
官方注释:Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).
翻译:在页面加载结束时被调用。
代码示例:
private long startTime;
private long endTime;
private long spendTime;
public void onPageFinished(WebView view, String url) {
endTime = System.currentTimeMillis();
spendTime = endTime - startTime;
Toast.makeText(view.getContext(), "spend time is:" + spendTime, Toast.LENGTH_SHORT).show();
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
startTime = System.currentTimeMillis();
}
4. onLoadResource(WebView view, String url)
官方注释:Notify the host application that the WebView will load the resource specified by the given url.
翻译:通知应用程序WebView将要加载指定url的资源,每一个资源(例如图片,嵌套url,js,css文件)。(可以通过该方法处理iframe嵌套的url)
代码示例:
public void onLoadResource(WebView view, String url) {
if (url.indexOf("http://www.example.com") != -1 && view != null) {
view.stopLoading();
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
百度云,自己看看。过段时间再来写。。
http://pan.baidu.com/s/1nu5M8a9
还有一些webview 的资料
深入讲解WebView——上|开源实验室-张涛
http://www.kymjs.com/code/2015/05/03/01
还一个没人的但是每天播放萌萌声音的微信公众号