2012-02-29 48 views
3

雖然我知道有關於這篇文章仍然有數百篇關於這個帖子的帖子,但是我沒有找到合適的答案。所以我在問這個問題。如何在一個完整的文本視圖中單詞可點擊android(不是url鏈接)

問題: - 「?你需要餅乾或香蕉吃」 我在文本視圖文本我想,如果任何一個點擊香蕉或餅乾,那麼只有它應該去廚房屏幕。如果點擊其他詞,它不應該反應。

我的嘗試: - 我正在從文本視圖,android:clickable="true",但完整的行是可以點擊。 所以我決定把它分解成不同的文字視圖,並使用相對的佈局,但是又出現了一個問題,即完整的線條不會進入屏幕的中心。當我將其硬編碼爲android:layout_marginLeft="150dp",但之後縮進會影響縱向和橫向。顯然我們不應該硬編碼值。除此之外,爲了給用戶一個清晰的視圖,我需要做一些格式化爲香蕉和餅乾,如粗體和下劃線以及某種顏色。儘管我使用了粗體和下劃線標記,但是如何爲文本着色。

最後我應該總結一下我的問題,因爲

1)如何使一些字在一個完整的字符串可點擊(不是URL鏈接)

2)如何做完整的字符串的一些文本的格式。

3)如果使用相對佈局,如何保持整個字符串居中。

如果有任何其他的方法可以B中他們也歡迎

一點要注意,這些都是不URL鏈接但內部屏幕。 所以如果有人可以幫忙請。 任何幫助將不勝感激。提前致謝。

回答

1

Hello AddingKnowledge。

你可以做的是在相對佈局內定義你的線性佈局,這將提供一定的靈活性。 除此之外,可以通過string.xml來完成。或者你可以從style.xml做樣式,這就是爲什麼style.xml在那裏。

如果你想要,你可以去鏈接爲How to make a part of text clickable like button in Android?瞭解一些更深層次的知識。或handle textview link click in my android app

+1

thanx的幫助爵士 – Android 2012-02-29 07:57:31

+1

日Thnx這有助於但沒有完全達到我的解決方案 – Android 2012-02-29 08:02:20

0

爲了保持在中央的文字試圖用機器人:layout_centerInParent = 「真」 DNT採用Android:layout_marginLeft = 「150dp」

和對於TextView中要點擊

   tv.setText(Html 
      .fromHtml("<a href='Banana'>Banana</a>")); 
    tv.setMovementMethod(LinkMovementMethod.getInstance()); 

    // Performing action on TouchListener of ForgotPassword 
    tvforgotpwd.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      Intent Forgotpwdintent = new Intent(); 
      Forgotpwdintent.setClass(getApplicationContext(), 
        Kicthenscreen.class); 
      startActivity(Forgotpwdintent); 
      return true; 

     } 
    }); 

看這個鏈接它可能有用...... http://coderzheaven.com/2011/05/textview-with-link-in-android/

XML文件,我試圖用相對佈局是..

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_toRightOf="@id/textView1" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_toRightOf="@id/textView2" 
     android:text="TextView" /> 
    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_toRightOf="@id/textView3" 
     android:text="TextView" /> 
</RelativeLayout> 

,我在Java文件寫這樣的..

  TextView tv=(TextView)findViewById(R.id.textView3); 
    tv.setText(Html.fromHtml("<a href='Banana'>Banana</a>")); 
    tv.setClickable(true); 
    tv.setMovementMethod(LinkMovementMethod.getInstance()); 
    tv.setOnTouchListener(new View.OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show(); 
      return true; 
     } 
    }); 

我的佈局對準唯一的中心..

+0

thanx快速回復,我會嘗試你的解決方案 – Android 2012-02-29 07:43:26

+0

抱歉不是android:layout_centerInParent =「true」使用android:layout_centerHorizo​​ntal =「true」。 – user1213202 2012-02-29 07:50:59

+0

android:layout_centerInParent =「true」 當我使用這個到我的文本視圖,它只是羚牛我的第一個textview中心,從而擾亂了佈局,同樣適用於android:layout_centerHorizo​​ntal =「true」 – Android 2012-02-29 07:55:34

0

嗨用戶AddingKnowledge,

我非常仔細地閱讀你的問題,如果你準備好打破句話在不同組的文本,那麼你可以在TextViews設置的文本,因爲我在below.You已經做可以檢查下面詳細介紹xml佈局。對於TextView text_banana和text_cookies,您可以設置onclick監聽器。

任何方便,請讓我知道。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

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

<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="You need " /> 

<TextView 
android:id="@+id/text_cookies" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="cookies" /> 

<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text=" or " /> 

<TextView 
android:id="@+id/text_banana" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="banana" /> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text=" to eat?" /> 

</LinearLayout> 

</RelativeLayout> 
+0

thanx快速回復,我會嘗試你的解決方案 – Android 2012-02-29 07:50:01

+0

這同樣適用於這個先生,它沒有中心對齊。任何方式thnkü答覆 – Android 2012-02-29 09:11:25

+0

如果要對齊屏幕中央的文本,以便更換的LinearLayout參數 機器人:layout_centerHorizo​​ntal =「真」 與 機器人:layout_centerInParent =「真」 – 2012-03-01 06:14:06

相關問題