android studio单击activity1中的按钮1以加载activity2 webview

android studio单击activity1中的按钮1以加载activity2 webview,android,android-layout,android-activity,Android,Android Layout,Android Activity,我的主要目标是在Activity1中单击一个按钮,然后在Activity2的WebView中本地从html文件加载页面 仅出于测试目的,我将只使用1个按钮,但就我个人而言,我将有超过1个按钮,每个按钮将导航到不同的页面 这是我在Activity1中打开Activity2 Intent intent = new Intent(this, MainActivity.class); startActivity(intent); 这就是我想加载一次的Activity2load

我的主要目标是在
Activity1
中单击一个按钮,然后在
Activity2
WebView
中本地从html文件加载页面

仅出于测试目的,我将只使用1个按钮,但就我个人而言,我将有超过1个按钮,每个按钮将导航到不同的页面

这是我在
Activity1
中打开
Activity2

Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
这就是我想加载一次的
Activity2
load

 setContentView(R.layout.activity_main);
                WebView view = new WebView(this);
                view.getSettings().setJavaScriptEnabled(true);
                view.loadUrl("file:///android_asset/index.html");
                view.setBackgroundColor(Color.TRANSPARENT);
                setContentView(view);
我可以用一个按钮来做这个好吗?但当我想在不同的页面(比如index.html2或index.html3)中使用不同的按钮时,问题就出现了。有人能帮我吗

谢谢。

请执行以下操作:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_1:
           Intent i = new Intent(this,MainActivity.this);
            i.putExtra("page","file:///android_asset/index1.html");
            startActivity(i);

            break;
        case R.id.btn_2:
            Intent i = new Intent(this,MainActivity.this);
            i.putExtra("page","file:///android_asset/index2.html");
            startActivity(i);

            break;
        case R.id.btn_3:
            Intent i = new Intent(this,MainActivity.this);
            i.putExtra("page","file:///android_asset/index3.html");
            startActivity(i);

            break;
    }

}
在您的另一项活动中
onCreate

setContentView(R.layout.activity_main);
String pageString = getIntent().getExtras().getString("page");
WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(pageString);
view.setBackgroundColor(Color.TRANSPARENT);
setContentView(view);`

你应该想想你的问题是什么

活动1中有一个按钮。 单击按钮启动Activity2并在那里执行一些特定的操作(如打开index1.html)

现在,您需要向Activity1添加更多按钮

每个按钮也应打开Activity2。但是每个按钮都应该做一些特定的事情(比如打开一个“index1.html”、“index2.html”或“index3.html”)

那你有什么问题? 您不知道如何告诉Activity2单击了哪个按钮,因此Activity2不知道打开哪个“index.html”

现在,让我告诉你怎么做:

您已经在使用此代码启动Activity2:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
但是一个
意图
可以比开始一个
活动
做得更多! 您可以将数据添加到意图对象中,稍后再将其取回

通过执行以下操作,将数据添加到您的意图中:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("htmlToOpen", "index1.html"); //Here you put your info
startActivity(intent);
Intent i = new Intent(this, Activity1.class);
startActivityForResult(i, 1);
Intent returnIntent = new Intent();
returnIntent.putExtra("htmlToOpen","index1.html"); //if another button was clicked, put another filename, here.
setResult(RESULT_OK,returnIntent);
finish();
现在,在activity2中,您必须返回数据:

String fileName = getIntent().getStringExtra("htmlToOpen");
现在,您可以使用字符串文件名打开所需的html文件:

setContentView(R.layout.activity_main);
WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/" + fileName);
view.setBackgroundColor(Color.TRANSPARENT);
setContentView(view);
要了解有关
Intent
s的更多信息,请阅读此处:


顺便说一句:我想你正在尝试做以下事情:

  • 您可以在
    main活动中启动应用程序
  • 你有一个按钮。用户可以单击按钮打开Activity1
  • 在Activity1中,用户可以选择一个网站(html文件)
  • 该网站现在应显示在
    main活动中
  • 如果这是您想要做的,让我建议Android的方法:

    main活动中
    ,通过执行以下操作启动活动1:

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("htmlToOpen", "index1.html"); //Here you put your info
    startActivity(intent);
    
    Intent i = new Intent(this, Activity1.class);
    startActivityForResult(i, 1);
    
    Intent returnIntent = new Intent();
    returnIntent.putExtra("htmlToOpen","index1.html"); //if another button was clicked, put another filename, here.
    setResult(RESULT_OK,returnIntent);
    finish();
    
    该代码意味着您启动活动1只是为了返回结果(在本例中,结果就是所需的网站)

    现在,在您的活动1中,通过执行以下操作返回结果(所需的网站):

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("htmlToOpen", "index1.html"); //Here you put your info
    startActivity(intent);
    
    Intent i = new Intent(this, Activity1.class);
    startActivityForResult(i, 1);
    
    Intent returnIntent = new Intent();
    returnIntent.putExtra("htmlToOpen","index1.html"); //if another button was clicked, put another filename, here.
    setResult(RESULT_OK,returnIntent);
    finish();
    
    现在,再次回到您的
    main活动
    ,通过添加以下函数获得结果:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String fileName = data.getStringExtra("htmlToOpen");
            //Now open your desired website by using the String fileName,
            //which contains the String "index1.html"
        }
    }
    }
    
    如果您想了解有关启动活动以获取结果的更多信息,请阅读:


    如何加载.html2或.html3?让我检查一下哇,先生,这看起来不错,让我试试,我不介意有人总是从新开始,对吗?我感谢您的主指南。@user3695970如果您还有其他问题(与我的答案有关),请在评论中询问。如果答案对您的问题有好处,您可以将其标记为“已接受答案”,当我发出这个命令时,你很高兴file:///android_asset/index1.html 或者activity2页面出现此字符串,但显示网页不可用显示此链接file:///android_asset/index1.html 有没有办法解决这个问题,因为我们知道这不是一个有效的url,而是一个指向本地化中html的路径。工作人员,非常感谢。