android - 如何添加指向 Switch 小部件文本的链接?

标签 android android-switch

我正在尝试进行 “接受条款和条件” 切换。我能够将链接添加到开关中,但问题是链接也在开关按钮上,所以每次我按下按钮接受条款和条件时,它都会将我带到网页。

我希望只有被选为链接的文本才是链接。

这是我现在的代码:

strings.xml 上:

<string name="terms">Accept<a href="http://www.google.es">terms & conditions</a>.</string>

register.xml 上:

<Switch
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/terms"
     android:id="@+id/registerSwitch"/>

Register.class 上:

Switch terms = (Switch) findViewById(R.id.registerSwitch);
terms.setMovementMethod(LinkMovementMethod.getInstance());

我怎样才能使文本 "terms & conditions" 成为链接而不是切换按钮?

最佳答案

为什么不将文本单独显示为 TextView 和 TextView 上的 setOnClickListener 以打开您的链接。有些像这样:

         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Switch
                android:id="@+id/your_switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/terms_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Accept terms &amp; conditions" />

        </LinearLayout>

Activity 中

TextView termsText = (TextView)findViewById(R.id.terms_text);
    termsText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
            startActivity(browserIntent);
        }
    });

关于android - 如何添加指向 Switch 小部件文本的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38766890/

相关文章:

android - RecyclerView onclick 不显示波纹效果或突出显示

java - 在 Android 上从字节流创建 pdf 文件

android - 带 Android 开关的 RxView

android - 切换轨道颜色

android - 错误 Android 模拟器在 Android Studio 中被杀死

android - 在 WebView 中隐藏滚动条

java - 将参数传递给 SOAP 方法

java - 如何对 switch 语句进行验证?

java - 如何以编程方式更改 Android Toggle/LabeledSwitch 边框颜色?

android - 开关 setOnCheckedChangeListener 无法正常工作