java - 如何根据用户在一个 Activity 中单击的按钮(三个中的一个)来更改单个 WebView (Android) 的内容?

标签 java android url webview onclick

您好,我是 Android 开发和 StackOverflow 的新手。

我有一个 Android WebView 应用程序,其中的“启动 Activity ”(只是介绍文本和启动主要 Intent 的按钮)启动我的 WebView Activity ,该 Activity 加载单个 URL(位于 onCreate 方法中)。

在此主要 Activity 中,我有三个按钮,其中一个在当前 Activity 中打开相同的 URL(有效地刷新页面)。另外两个按钮我想在同一个 WebView Activity 中打开两个不同的 URL,我想知道这是否/如何可能。

这是主要的 ReadWebpageAsyncTask.java Activity 代码:

public class ReadWebpageAsyncTask extends Activity {

  private WebView browser;
  protected String urlStrOne = "url one";
  protected String urlNewCourses = "url two";
  protected String urlFutureCourses = "url three";


  @Override     
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_read_webpage_async_task);
  browser = (WebView)findViewById(R.id.WebView01);
  browser.setWebViewClient(new MyBrowser());
  browser.loadUrl(urlStrOne);
  }


  @SuppressLint("SetJavaScriptEnabled")
  public void onClick(View view){
  browser.getSettings().setLoadsImagesAutomatically(true);
  browser.getSettings().setJavaScriptEnabled(true);
  browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
  browser.loadUrl(urlStrOne);
  }


  private class MyBrowser extends WebViewClient {
  @Override
  public boolean shouldOverrideUrlLoading(WebView browser, String urlStrOne) {
     browser.loadUrl(urlStrOne);
     return true;
  }
  }   

  @Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if(browser.canGoBack()) {
            browser.goBack();
        } else {
       // Let the system handle the back button
       super.onBackPressed();
        }
  }
  }

以及相应的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >

<RelativeLayout 
android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black">

<Button
    android:id="@+id/readWebpage"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/soon"
    android:background="@+drawable/custom_button_one"
    android:onClick="onClick"
    android:text="@string/load_page" />

<Button
    android:id="@+id/exitapp01"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/soon"
    android:background="@+drawable/custom_button_one"
    android:onClick="exitButton"
    android:text="@string/exit_button" />

<Button
    android:id="@+id/soon"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_toLeftOf="@+id/exitapp01"
    android:layout_toRightOf="@+id/readWebpage"
    android:layout_alignParentTop="true"
    android:onClick="comingSoon"
    android:text="@string/future_courses" />

    </RelativeLayout>

<WebView
    android:id="@+id/WebView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/WebView01" >

</WebView>
</LinearLayout>

附注请原谅所有内容的糟糕命名,我不久前写了这篇文章,但还没有抽出时间来更改它。

澄清一下,首先加载的主 URL 与“readWebpage”按钮相同,我想在单击时加载的另外两个 URL 是“exitapp01”按钮(我能说什么,我是菜鸟)和“很快”。

最佳答案

您可以将 OnClickListener 添加到按钮并根据情况切换 url:

public class ReadWebpageAsyncTask extends Activity implements OnClickListener{

    private WebView browser;
    protected String urlStrOne = "url one";
    protected String urlNewCourses = "url two";
    protected String urlFutureCourses = "url three";
    Button reload, exit, soon;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_webpage_async_task);
        browser = (WebView)findViewById(R.id.WebView01);
        browser.setWebViewClient(new MyBrowser());
        browser.loadUrl(urlStrOne);

        reload = (Button)findViewById(R.id.readWebpage);
        exit = (Button)findViewById(R.id.exitapp01);
        soon = (Button)findViewById(R.id.soon);

        reload.setOnClickListener(this);
        exit.setOnClickListener(this);
        soon.setOnClickListener(this);
    }


    @SuppressLint("SetJavaScriptEnabled")
    public void onClick(View view){

    }

    @Override
    public void onClick(View view) {

        switch(view.getId()) {

            case R.id.readWebpage:
                browser.getSettings().setLoadsImagesAutomatically(true);
                browser.getSettings().setJavaScriptEnabled(true);
                browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                browser.loadUrl(urlStrOne);
                break;
            case R.id.exitapp01:
                //Do whatever you want with this button
                break;
            case R.id.soon:
                //Do whatever you want with this button
                break;

        }
    }
}

并从 xml 中删除所有 android:onClick=""

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="left|right"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black">

        <Button
            android:id="@+id/readWebpage"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"/>

        <Button
            android:id="@+id/exitapp01"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_centerInParent="true"/>

        <Button
            android:id="@+id/soon"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>

    <WebView
        android:id="@+id/WebView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

关于java - 如何根据用户在一个 Activity 中单击的按钮(三个中的一个)来更改单个 WebView (Android) 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24960478/

相关文章:

java - 如何使哈希表中的一个键具有多个值?

java - ANTLR 中使用监听器的 if/else 语句

java - 如何在 Websphere V7 中以编程方式在运行时修改跟踪服务?

android - 无法使用 Firebase 控制台发送数据消息

java - GWT 的 Speed Tracer 服务器端跟踪

java - 如何将 map 添加到选项卡式 Activity (Android)

javascript - 如何将页面 URL 中的井号更改为其他值?

javascript - Laravel:我在 Controller 中编写的 URL 在 View 中不起作用

php - 如何构建 public_html 文件夹

java - 从 zip 创建文件 - android