开发者

I need exact simple instructions for placing a simple hyperlink to a web site in a java android application

I am a PHP programmer who is having to do some work in the android development environment. I have 2 books on this and have tried 30 search engine topics and still have not 开发者_StackOverflowfound just a simple example of everything that you need to do to place a working hyperlink in a Java android application. I just need a very simple but complete ingredient for doing so. I have the 2.2 android development environment with Eclipse and an emulator. I have tried the WebView control which just simply loads a web site into the window when I run the application. I need a basic hyperlink to a web site example. I don't want anything else thrown in with it (just an application with a working hyperlink and nothing else), because I am trying to learn the different controls bit by bit along with the Java and XML code that controls them. This is so different from PHP, ASP, etc. that it has me totally fishing for answers. Thanks;

Cullan


Android is a GUI, not a Web browser. Hence, "place a working hyperlink in a Java android application" is akin to "place a snowplow blade on a dishwasher" or "implement a Web app in COBOL". It is technically possible but probably is the incorrect solution to whatever problem it is that you really trying to solve.

So, as MatrixFrog indicates, one possibility is to use a TextView and some HTML:

TextView tv=(TextView)findViewById(R.id.whatever_you_called_it_in_your_layout);

tv.setText(Html.fromHtml("<a href=\"http://foo.com/why/are/we/doing/this\">Who knows?</a>"));

But, doing that would be unusual in a GUI environment. Most developers would use a button, or a menu choice, or something along those lines, to trigger viewing some URL.


CommonsWare, That is not what I call a detailed explanation or example of how to place a hyperlink inside an android application. It is just a small snippet of code with no further explanation. I found what works on my own and here is the Java code for it:

package com.practice.weblink;

import android.app.Activity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.widget.TextView;

public class WebLink extends Activity 
{
@Override

 public void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 TextView textview = (TextView) findViewById(R.id.hyperlink);
 Linkify.addLinks(textview, Linkify.WEB_URLS);
 }
}

The TextView has the following qualities in the main.xml file:

<TextView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="@string/hyperlink"
android:id="@+id/hyperlink" 
android:autoLink="web"
>
</TextView>

The strings.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebLink</string>
<string name="hyperlink">http://google.com</string>
</resources>

That is how you give a working example of something. Next time don't assume that people can just piece together what you are mentioning in your answer.


How about using onClick in the XML layout file?

layout.xml

 <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:clickable="true"
    android:text="@string/market_url"
    android:textColor="#00f"
    android:onClick="openURL"
    />

MyActivity.java

public void openURL(View v) {
    String url = ((TextView) v).getText().toString();
    final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
    startActivity(intent);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜