简体   繁体   English

Android WebView刷新页面并显示Toast [HW]

[英]Android WebView Refresh Page and Display Toast [HW]

This is for study and I just want to make sure my answers are 100% right. 这是为了学习,我只想确保我的答案是100%正确。 The question gives me skeleton code and I'm required to fill it in. Here is the code. 这个问题给了我基本代码,我需要填写它。这是代码。

public class WebFragment extends WebViewFragment {

   private WebView mWebView;

   @Override
   public void onActivityCreated(Bundle.savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      // Fill in here
   }

   public void refreshPage(View view) {
       // Fill in here
   }

}

Now I am asked three questions. 现在问我三个问题。 I have put my answers below each question. 我已将我的答案放在每个问题的下面。

a. 一种。 Instantiate the WebView property in the class 实例化类中的WebView属性

b. b。 When the activity has started, make the WebView component load the url " http://www.google.com/ " 活动开始后,使WebView组件加载网址“ http://www.google.com/

   @Override
   public void onActivityCreated(Bundle.savedInstanceState) {
      super.onActivityCreated(savedInstanceState);

      mWebView = (WebView) findViewById(R.id.webview);  // question a
      mWebView.getSettings().setJavaScriptEnabled(true); // question a
      mWebView.loadUrl("http://www.google.com");  // question b
   }

c. C。 Assuming a button is available in the activity layout and runs refreshPage when pressed, make the function reload the WebView page and show a Toast with the message "Page has been refreshed!" 假设有一个按钮在活动布局中可用,并且在按下该按钮时运行refreshPage,使该函数重新加载WebView页面并显示带有消息“页面已刷新!”的Toast。

   public void refreshPage(View view) {
       mWebView.reload();
       Toast toast = Toast.makeText(getApplicationContext(), "Page has been refreshed!"), Toast.LENGTH_SHORT().show();

   }

Any feedback is appreciated. 任何反馈表示赞赏。

answer a seems ok, for the toast you don't need the variable, unless you don't want to do something other than showing the toast 回答似乎还可以,对于吐司,您不需要该变量,除非您不想做其他事而不是展示吐司

mWebView.reload();
Toast.makeText(getApllicationContext(),"foo",Toast.LENGTH_SHORT).show();

or pass a custom time in milliseconds 或通过自定义时间(以毫秒为单位)

Toast.makeText(getApllicationContext(),"foo",2000).show();

you can also initialize a Toast variable and than call show on it, it works, it's just a useless variable if you're not going to do anything with it but it's not wrong 您还可以初始化Toast变量,然后调用show,它起作用了,如果您不打算对其进行任何操作,那么它只是一个无用的变量,但这没错

Toast toast = Toast.makeText(getApplicationContext(),"foo",1000);
toast.show();

oh, I don't know if it's required, but like this refreshPage method is not callable by anybody, maybe you want to set an OnClickListener to the button (that we assume is in the layout) so that refreshPage can be called 哦,我不知道是否需要它,但是像这样的refreshPage方法不能被任何人调用,也许您想为按钮设置一个OnClickListener(我们假设它在布局中),以便可以调用refreshPage

((Button)findViewById(R.id.buttonId)).setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        refreshPage(v);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM