如何使一个复选框的文本可点击的部分? [英] How do I make a portion of a Checkbox's text clickable?

查看:228
本文介绍了如何使一个复选框的文本可点击的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立在我的文本框的旁边的文本链接。然而,这个环节是不是一个网址,而是应该作为一个按钮,这样我可以在onItemClick事件中执行一些任务。我基本上连这一个视图,显示我们的最终用户许可协议(硬codeD)。

我怎样才能做到这一点?

在此先感谢。

解决方案

目前实际上是一个优雅的解决方案,使用复选框和单的TextView 。随着一个组合 TextView.setClickable(),意图过滤器,和 TextView.setMovementMethod()

您有主视图(在这里,我把它叫做 ClickableTextViewExample ):

 包id.web.freelancer.example;

进口android.app.Activity;
进口android.os.Bundle;
进口android.text.Html;
进口android.text.method.LinkMovementMethod;
进口android.widget.CheckBox;
进口android.widget.TextView;

公共类ClickableTextViewExampleActivity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);


        复选框复选框=(复选框)findViewById(R.id.checkBox1);
        TextView中的TextView =(TextView中)findViewById(R.id.textView2);

        checkbox.setText();
        textView.setText(Html.fromHtml(我已阅读并同意+
                < A HREF ='id.web.freelancer.example.TCActivity:// KODE'>使用条件< / A>中));
        textView.setClickable(真正的);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
 

的main.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的LinearLayout
        机器人:ID =@ + ID / linearLayout1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>

        <复选框
            机器人:ID =@ + ID / checkBox1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=复选框/>

        <的TextView
            机器人:ID =@ + ID / textView2
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=TextView的
            机器人:可点击=真/>

    < / LinearLayout中>

< / LinearLayout中>
 

TCActivity.java

 包id.web.freelancer.example;

进口android.app.Activity;
进口android.os.Bundle;

公共类TCActivity延伸活动{

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.tc);
    }

}
 

tc.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

        <的TextView
        机器人:ID =@ + ID / tcView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=条款及条件/>


< / LinearLayout中>
 

和最后一张codeS的胶水这一切,在的Andr​​oidManifest.xml 的:

 <活动机器人:名称=TCActivity>
    <意向滤光器>
        <类机器人:名称=android.intent.category.DEFAULT/>
    <作用机器人:名称=android.intent.action.VIEW/>
    <数据机器人:计划=id.web.freelancer.example.TCActivity/>
    &所述; /意图滤光器>
< /活性GT;
 

下面来了,说明:

  textView.setText(Html.fromHtml(我已阅读并同意+
                     < A HREF ='id.web.freelancer.example.TCActivity:// KODE'>使用条件< / A>中));
textView.setClickable(真正的);
textView.setMovementMethod(LinkMovementMethod.getInstance());
 

  

setClickable 将允许您点击的TextView 。但不中的HREF链路。要做到这一点,你将不得不使用 setMovementMethod()键,将其设置为 LinkMovementMethod

在这之后,你需要捕捉的URL。我这样做是使用的意图过滤器 AndroidManifest.xml中

 <作用机器人:名称=android.intent.action.VIEW/>
<数据机器人:计划=id.web.freelancer.example.TCActivity/>
 

  

据赶上VIEW命令,它只能过滤网址开头 id.web.freelancer.example.TCActivity://

<一个href="http://dl.dropbox.com/u/1636195/gambar_stackoverflow_jangan_delete/ClickableTextViewExample.zip"相对=nofollow>这里的包,你试试吧和这里GitHub的资源库。希望这有助于

I'm trying to create a link in my textbox's adjacent text. This link however is not a URL, but should act as a button so that I can perform a few tasks in the onItemClick event. I'm basically connecting this to a view that shows our End User License Agreement (hard coded).

How can I accomplish this?

Thanks in advance.

解决方案

There actually is an elegant solution, using CheckBox and single TextView. Along with a combinations of TextView.setClickable(), Intent Filter, and TextView.setMovementMethod().

You have main view (here, I called it ClickableTextViewExample):

package id.web.freelancer.example;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.CheckBox;
import android.widget.TextView;

public class ClickableTextViewExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        CheckBox checkbox = (CheckBox)findViewById(R.id.checkBox1);
        TextView textView = (TextView)findViewById(R.id.textView2);

        checkbox.setText("");
        textView.setText(Html.fromHtml("I have read and agree to the " +
                "<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>"));
        textView.setClickable(true);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckBox" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:clickable="true" />

    </LinearLayout>

</LinearLayout>

TCActivity.java

package id.web.freelancer.example;

import android.app.Activity;
import android.os.Bundle;

public class TCActivity extends Activity {

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

}

tc.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <TextView
        android:id="@+id/tcView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Terms and conditions" />


</LinearLayout>

and the final piece of codes that glue it all, the AndroidManifest.xml:

<activity android:name="TCActivity">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="id.web.freelancer.example.TCActivity" />  
    </intent-filter>            
</activity>

Here comes, the explanations:

textView.setText(Html.fromHtml("I have read and agree to the " +
                     "<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>"));
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());

setClickable will allow you to click on textView. But not the HREF link. To do that, you will have to use setMovementMethod() and set it to LinkMovementMethod.

After that, you need to catch the URL. I did this using intent-filter in AndroidManifest.xml

<action android:name="android.intent.action.VIEW" />
<data android:scheme="id.web.freelancer.example.TCActivity" />  

It catch VIEW command and it only filter URL starting with id.web.freelancer.example.TCActivity://

Here's the package for you to try it out and here's the github repository. Hope this helped

这篇关于如何使一个复选框的文本可点击的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆