Android: Image button as a hyperlink, phone call,

2020-07-24 03:39发布

问题:

I have a simple app that I'm putting together for my company. I have 4 buttons that I've created but can't seem to get them to link correctly. One button should open our mobile site, another button to call us, another button to map to us, and the final button linked to our "News" site. Any help would be greatly appreciated!

回答1:

On your buttons, you should set OnClickListener, and to do some required actions you could see the example below:

  1. To Open a Map with Certain Location

    mapButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + your-location-geo-address));
            startActivity(i);
        }
    });
    
  2. To call certain number

    callButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + telephone-number));
            startActivity(i);
        }
    });
    
  3. To open a website

    linkButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(website-address));
            startActivity(i);
        }
    });
    

Change "location-address", "telephone-number", and "website-address" with your own String value. I hope this helps.



回答2:

anmustangs answer is very good, but one thing I would like to add for the button you are making for a link to your site, where anmustangs wrote (website-address) instead of just typing in a site, it needs to put formatted correctly. For example, you can use "http://www.google.com" and yes you do need to use the quotation marks I put in there. I know I am years late to this thread but who knows who my post may help.