【问题标题】:How to link 3 buttons to 2 different html links and third button to another activity in eclpse?如何将 3 个按钮链接到 2 个不同的 html 链接,将第三个按钮链接到 eclipse 中的另一个活动?
【发布时间】:2023-03-29 05:19:01
【问题描述】:

我创建了 2 个按钮,我想将它们链接到 2 个不同的 html 链接,但我只能使用下面的代码链接一个....

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.content.Intent;
    import android.net.Uri;

    public class Main extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button btn = (Button) findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
                    myWebLink.setData(Uri.parse("http://........."));
                        startActivity(myWebLink);
                 }
            });

    }

Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent myWebLink2 = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink2.setData(Uri.parse("http://link2."));
                    startActivity(myWebLink2);
             }
        });

我知道如何将按钮 2 链接到另一个链接,但现在我需要按钮 3 在我们单击它时启动下一个活动,如何????

如果有要导入或创建类的东西,请给我一步一步的详细信息.....

提前致谢。

【问题讨论】:

标签: android eclipse android-activity eclipse-adt


【解决方案1】:

查看the official tutorial for that。基本上,在您的第三个按钮中,您需要使用适当的意图调用 startActivity,例如:

Button btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Main.this, SecondActivity.class);
        startActivity(intent);
        }
    });

SecondActivity 是您的第二个活动的名称,因此请将其替换为您需要的任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2017-09-30
    • 2019-01-27
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多