简体   繁体   English

android:单击按钮时显示html页面?

[英]android: display html pages when button clicked?

FULLY UPDATED: 完全更新:

I have 100 html files. 我有100个html文件。 I know how to initialize the <WebView /> for single xml page. 我知道如何为单个xml页面初始化<WebView /> But, If I used this method, then i need to create 100 xml pages. 但是,如果使用此方法,则需要创建100个xml页面。 So, its waste of time. 因此,浪费时间。

So, I created <WebView> in web.java and 100 buttons in chapters.java 所以,我创建<WebView>web.java和100层的按钮chapters.java

What i am asking is, if button1 pressed, chapter1.html should be open in web.java . 我所问的是,如果button1压制, chapter1.html应该是开放web.java if button2 pressed, chapter2.html should be open in web.java . 如果button2压制, chapter2.html应该是开放web.java like all 100 files should be open in web.java ? 像所有100个文件应该在web.java打开?

My XML Code: 我的XML代码:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:text="Button1"
    android:onClick="Button1"/>

.... 100 TextView .... 100文字检视

My JAVA code: 我的JAVA代码:

public void Button1(View v) {
WebView wv;  
wv = (WebView) findViewById(R.id.webview);  
wv.loadUrl("file:///android_asset/chapter1.html"); 
}

.... 100 OnClick method. .... 100 OnClick方法。

This will never work as your textViews have no Id s 这将永远无法工作,因为您的textViews没有Id

try to add this proprety android:id="@+id/yourId" to each of your textViews yourId will take values from button1 to button5 尝试将此proprety android:id="@+id/yourId"到您的每个textView中,您的id将值从button1button5

Use only one onclick method in all the textView: 在所有textView中仅使用一种onclick方法:

then call each of them like that 然后这样叫他们每个人

void onclick(View v)
{
    switch(v.getId())
    {
    case R.id.button1:
    //Code will be executed when you click on Button1
    break;

    case R.id.button2:
    //Code will be executed when you click on Button2
    break;

    case R.id.button3:
    //Code will be executed when you click on Button3
    break;
    .
    .
    .
    .
    default:
    break;
    }
 }

Update 更新资料

The following code worked fine for me: 以下代码对我来说很好用:

activity_main.xml activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="234dp" >

    <LinearLayout
        android:layout_width="321dp"
        android:id="@+id/myLinearLayout"
        android:layout_height="154dp"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="224dp" />

</LinearLayout>

MainActivity.java MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
Button [] myButton = new Button[100];
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    for(int i=0;i<100;i++)
    {
        myButton[i] = new Button(this);
        myButton[i].setOnClickListener(this);
        myButton[i].setText("Button"+i);
        LinearLayout ll = (LinearLayout)findViewById(R.id.myLinearLayout);
        LayoutParams lp = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(myButton[i], i);

    }

}
@Override
public void onClick(View v) 
{
    for(int i=0;i<myButton.length;i++)
    {
        if(myButton[i]==v)
        {
        String s=Integer.toString(i);
        wv=(WebView)findViewById(R.id.webView);
        wv.loadUrl("file:///android_asset/chapter1.html"+s);
        }

    }


}
}

Make sure you add the following line to your AndroidManifest.xml: 确保将以下行添加到AndroidManifest.xml中:

<uses-permission android:name="android.permission.INTERNET" />

Demo 演示版

在此处输入图片说明

There is no WebView in your XML File . 您的XML文件中没有WebView。 Add it . 添加它。 And take reference of that in the Oncreate only. 并仅在Oncreate中引用它。 Now u single WebView wv with all the methods. 现在,您可以通过所有方法使用单个WebView wv。

      Public Class MainActivity extends Activity{
      WebView wv;

      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      wv = (WebView) findViewById(R.id.webview);  
      }
      }

try this the loader page class: 试试这个加载器页面类:

    Package ...; //name of your package.
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class web extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

            WebSettings webSettings = mainWebView.getSettings();
            webSettings.setJavaScriptEnabled(true); //enables java script

            mainWebView.setWebViewClient(new MyCustomWebViewClient());
            mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

            mainWebView.loadUrl(" Your url "); // type your url here.
        }

        private class MyCustomWebViewClient extends WebViewClient {

           @Override

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }
    }

Xml file: xml文件:

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

Manifest file: 清单文件:

<uses-permission
         android:name="android.permission.INTERNET"
         ></uses-permission>

New code: add this code in the Activity which sets the layout where your buttons are 新代码:将此代码添加到“活动”中,以设置按钮所在的布局

    Button button15 =(Button)findViewById(R.id.button5);
           button15.setOnClickListener(new View.OnClickListener() {


        public void onClick(View view) {

            Intent p = new Intent (Activity.this,
                               Webds.class);

            }
        }
        );
}

webds class : add this webds类:添加此

mWebview = new WebView(this);
mWebview.loadUrl("http://www.google.com");
setContentView(mWebview);

There is a mistake in JAVA CODE. JAVA CODE中有一个错误。 Just replace it with my code. 只需将其替换为我的代码即可。

public void webClick(View v) 
        {
            switch(v.getId())
            {
            case R.id.button1:
                Intent intent = new Intent(this, Webview.class);
                intent.putExtra("weblink","file:///android_asset/chapter/chapter1.html");
                startActivity(intent);
                break;

.
.
.
.
            default:
            break;
            }
         }

And create Webview.java with the following code; 并使用以下代码创建Webview.java;

public class Webview extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);; 
        String webLink = getIntent().getStringExtra("weblink");
        WebView wv;  
        wv = (WebView) findViewById(R.id.webview);  
        wv.loadUrl(webLink);

    }

So, for every call, your respective html file will be opened in the same browser. 因此,对于每个呼叫,您各自的html文件将在同一浏览器中打开。 Webview.java . Webview.java hope, it helps you. 希望对您有帮助。 don't fail to mark this as answer if it helps you. 如果有帮助,请务必将其标记为答案。

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

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