我正在尝试进行 “接受条款和条件”
切换。我能够将链接添加到开关中,但问题是链接也在开关按钮上,所以每次我按下按钮接受条款和条件时,它都会将我带到网页。
我希望只有被选为链接的文本才是链接。
这是我现在的代码:
在 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 & 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/