2016-08-04 84 views
0

我正在尝试做一个"Accept terms and conditions"开关。我能够将链接添加到交换机中,但问题是链接也在交换机按钮上,所以每当我按下按钮接受条款和条件时,它就会将我带到网页中。如何添加链接到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"将是链接而不是开关按钮?

+0

你可以试试开关和TextView的独立 –

+0

请遵循命名约定。它应该是RegistrationActivity而不是Register。见这个页面 - https://github.com/ribot/android-guidelines/blob/master/project_and_code_guidelines.md – Sufian

+0

@Sufian感谢您的建议。其实我把它作为我的真实项目的RegistrationActivity,但是当我将它翻译成英文时,我只把它作为注册在这里。对困惑感到抱歉。 –

回答

1

为什么不在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> 

在活动

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); 
     } 
    }); 
+0

我没有用两个元素来做,因为我认为它可能是有些方法只用一个元素来完成,我使用'terms.setMovementMethod(LinkMovementMethod.getInstance());'而不是'onClick'事件,谢谢! –

0

尝试添加机器人:linksClickable

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

这不起作用。 .getInstance());'离开'android:linksClickable =“true”'开关按钮工作正常,但链接停止工作 –

0

试试下面的代码

<Switch 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/swtch" 
    android:text="@string/terms" 
    android:linksClickable="true" 
    android:textIsSelectable="true" 
    android:autoLink="web"/> 

中的strings.xml

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

终于在活动

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

它不起作用,它只关注交换机,但它也通过你到链接你按下开关按钮 –

+0

然后你必须使用** Textview **和** Switch **分开并建立** Textview **的链接 –

相关问题