2016-08-04 18 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 **的鏈接 –

相關問題