使用eclipse在android中的可点击链接

时间:2015-05-23 11:23:35

标签: android json eclipse textview onclicklistener

我正在尝试在文本视图中设置字符串clickable.My textview包含各种在线问题的名称,我希望他们在点击时将用户重定向到问题的网址。任何人都可以推荐更改我的代码。

在下面的代码中,“结果”是由问题名称组成的最终文本视图。

public class Http extends Activity {

TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://codeforces.com/api/user.status?handle=";
String m = "";
public static  String[] sarr = new String[200];
public static String[] name = new String[200];
public static int cnt = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpex);

    httpStuff = (TextView)findViewById(R.id.tvHttp);
    client = new DefaultHttpClient();
    new Read().execute("result");
}

public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException {
    StringBuilder url = new StringBuilder(URL);
    url.append(username);
    HttpGet get = new HttpGet(url.toString());
    int status = 0;
    HttpResponse r = client.execute(get);
    status = r.getStatusLine().getStatusCode();
    if(status == 200) {
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONObject last = new JSONObject(data);
        //JSONObject last = new JSONObject(data).getJSONArray("result").getJSONObject(0).getJSONObject("problem");

        return last;
    }
    else {
        Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT);
        return null;

    }
}

public class Read extends AsyncTask <String, Integer, String> {

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        try {
            String add = "&from=1&count=100";
            String input = (Mainapp.p);
            input = input.concat(add);
            json = lastSub(input);
            JSONArray array = json.getJSONArray("result");
            String m1=null, m2=null, m3=null;
            String n1 = System.getProperty("line.separator");
            int flag=0;
            for(int k=0; k<array.length() && cnt<15; k++)
            {
                JSONObject json1 = array.getJSONObject(k).getJSONObject("problem");
                JSONObject v = array.getJSONObject(k);
                if(v.getString("verdict").contentEquals("OK")) {
                    m1 = json1.getString("name");
                    m2= json1.getString("contestId");
                    m2= m2.concat("/");
                    m3= json1.getString("index");
                    m2 = m2.concat(m3);
                    flag = 0;
                    for(int i=0; i<cnt ; i++) {
                        if (sarr[i].equals(m1)) {
                            flag = 1;
                        }
                    }
                    if(flag == 0) {
                        m = m.concat(m1);
                        m= m.concat(n1);
                        sarr[cnt] = m1;
                        name[cnt] = m2;
                        cnt++;
                    }
                }
            }
            return m;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RuntimeException e) {
            e.printStackTrace();

        }
        return "INVALID USER NAME";
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
            httpStuff.setText(result);
        }
    }



  }

}

2 个答案:

答案 0 :(得分:1)

您想使用问题名称作为超链接

String html = "<html> Visit <a href=\"http://codeforces.com/api/question1\">Question1</a> </html>";
textview.setText(Html.fromHtml(html));
textview.setMovementMethod(LinkMovementMethod.getInstance());

答案 1 :(得分:0)

此代码不仅可以让链接可点击,而且#和@可点击

创建一个名为InternalURLSpan.java的新文件并将此代码粘贴到其中:

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;


public class InternalURLSpan extends android.text.style.ClickableSpan {
    public String text;

    public static Context context;

    public InternalURLSpan(Context contex){
        this.context = context;     
    }

    @Override
    public void onClick(View widget) {
        handleLinkClicked(text, widget.getContext());
    }

    public void handleLinkClicked(String value, Context context) {
        if (value.startsWith("http")) {     
            // Open the url with the default webpage
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(value));
            context.startActivity(i);

        } else if (value.startsWith("@")) { 
            value = value.replace("@", ""); //To get the text without the @

            System.out.println(value);





        } else if (value.startsWith("#")) { 
            value = value.replace("#", ""); //To get the text without the #

            System.out.println(value);


        }
    }

}

将其粘贴到一个类中(可以是相同的)

void setLinks(TextView tv, String text) {
        String[] linkPatterns = {
                "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",
                "#[\\w]+", "@[\\w]+" };
        for (String str : linkPatterns) {
            Pattern pattern = Pattern.compile(str);
            Matcher matcher = pattern.matcher(tv.getText());
            while (matcher.find()) {
                int x = matcher.start();
                int y = matcher.end();
                final android.text.SpannableString f = new android.text.SpannableString(
                        tv.getText());
                InternalURLSpan span = new InternalURLSpan(context);
                span.text = text.substring(x, y);
                f.setSpan(span, x, y,
                        android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(f);
                // tv.setOnLongClickListener(span.l);

            }
        }
        tv.setLinkTextColor(Color.BLUE);
        tv.setLinksClickable(true);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setFocusable(false);
    }

然后使用:

setLinks(textView, "THE TEXT WHERE THE LINK IS IN, FOR EX. http://www.google.nl will be underlined and clickable #this_will_also_be_clickable @this_is_clickable_too");