How to link 2 buttons to 2 different html links in eclpse?

人走茶凉 提交于 2019-12-14 04:12:17

问题


I have created 2 buttons and i want to link both of them to 2 different html links,but i could link only one by using this below code....

package com.kk24.adding two buttons;

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);
             }
        });

}

Now i want to link button 2 to another link how do we link ????

Give me step by step details if there is something to import or creating a class or so.....

Thanks in advance.


回答1:


Try this code:

@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://link1."));
                    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:


create new String stringUris then make the String stringUris equals the first link in the first button and in the second button equals the second link then start the activity

    String stringUris;
    @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) {
                stringUris = "http://www.example1.com";
                Intent Intent1 = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink.setData(Uri.parse(stringUris));
                    startActivity(myWebLink);
             }
        });

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

             }

you can use a class for showing webview within the app if you want the class contact me.




回答3:


On xml file in each button add two attributes android:tag with the url and android:onClick with the method name that handle the event

<Button android:id="@id/btSite1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="http://site_1.com" 
    android:onClick="openBrowser"/>

<Button android:id="@id/btSite2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="http://site_2.com"
    android:onClick="openBrowser"/>  

On activity declare the method openBrowser to handle the click event:

public class Main extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void openBrowser(View view){

        //Get url from tag
        String url = (String)view.getTag();
        if(url != null){

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);

            //pass the url to intent data
            intent.setData(Uri.parse(url));

            startActivity(intent);
        }
    }
}  

Now when an button is clicked the openBrowser method is called and the browser is open.



来源:https://stackoverflow.com/questions/21027895/how-to-link-2-buttons-to-2-different-html-links-in-eclpse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!