如何使复选框文本的一部分可点击? [英] How do I make a portion of a Checkbox's text clickable?
问题描述
我正在尝试在文本框的相邻文本中创建链接.但是,此链接不是 URL,而是应充当按钮,以便我可以在 onItemClick 事件中执行一些任务.我基本上将其连接到显示我们的最终用户许可协议(硬编码)的视图.
我怎样才能做到这一点?
提前致谢.
实际上有一个优雅的解决方案,使用 CheckBox
和单个 TextView
.连同 TextView.setClickable()
、Intent Filter 和 TextView.setMovementMethod()
的组合.
你有主视图(在这里,我称之为ClickableTextViewExample):
package id.web.freelancer.example;导入 android.app.Activity;导入 android.os.Bundle;导入 android.text.Html;导入 android.text.method.LinkMovementMethod;导入 android.widget.CheckBox;导入 android.widget.TextView;公共类 ClickableTextViewExampleActivity 扩展 Activity {/** 在第一次创建活动时调用.*/@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);CheckBox 复选框 = (CheckBox)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(true);textView.setMovementMethod(LinkMovementMethod.getInstance());}}
main.xml
<线性布局android:id="@+id/linearLayout1"android:layout_width="match_parent"android:layout_height="wrap_content" ><复选框android:id="@+id/checkBox1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="CheckBox"/><文本视图android:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:文本=文本视图"android:clickable="true"/></LinearLayout></LinearLayout>
TCActivity.java
package id.web.freelancer.example;导入 android.app.Activity;导入 android.os.Bundle;公共类 TCActivity 扩展了 Activity {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tc);}}
tc.xml
<文本视图android:id="@+id/tcView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="条款和条件"/></LinearLayout>
以及将这一切粘合在一起的最后一段代码,AndroidManifest.xml:
<意图过滤器><category android:name="android.intent.category.DEFAULT"/><action android:name="android.intent.action.VIEW"/><data android:scheme="id.web.freelancer.example.TCActivity"/></意图过滤器></活动>
来了,解释:
textView.setText(Html.fromHtml("我已阅读并同意" +"<a href='id.web.freelancer.example.TCActivity://Kode'>条款和条件</a>"));textView.setClickable(true);textView.setMovementMethod(LinkMovementMethod.getInstance());
<块引用>
setClickable
将允许您点击 textView
.但不是 HREF 链接.为此,您必须使用 setMovementMethod()
并将其设置为 LinkMovementMethod
.
之后,您需要捕获 URL.我使用 AndroidManifest.xml
中的intent-filter
做到了这一点 <data android:scheme="id.web.freelancer.example.TCActivity"/>
<块引用>
它捕获 VIEW 命令,它只过滤以 id.web.freelancer.example.TCActivity://
这是供您试用的软件包 和
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 ontextView
. But not the HREF link. To do that, you will have to usesetMovementMethod()
and set it toLinkMovementMethod
.
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屋!