2011-01-06 14 views
11

私はクリック可能にしたい多くのTextViewsを持つAndroid Appを構築しています。 プロパティandroid:clickable="true"android:onClick="clickHandler"を単一のTextViewに割り当てようとしましたが、アプリケーションがclickHandler(View v)を起動すると、クリックされたアイテムが正しくv.getId()に届きました。 私が気に入らないのは、TextViewごとに、プロパティーandroid:clickableと ...を定義することです。「すべてのテキストビューをクリック可能でクリックはclickHandlerで処理しますか?AndroidのClickable TextView

ありがとうございました。

答えて

16

あなたは以下のこのような何かを行うことができます - それらはすべて同じハンドラを持ってこの方法:OnTouchListener.Hereはa description how to use themあるの

public class sticks extends Activity implements View.OnTouchListener { 
    private TextView tv1; 
    private TextView tv2; 
    private TextView tv3; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tv1 = (TextView) findViewById(R.id.tv1); 
    tv2 = (TextView) findViewById(R.id.tv2); 
    tv3 = (TextView) findViewById(R.id.tv3); 

    // bind listeners 
    tv1.setOnTouchListener(this); 
    tv2.setOnTouchListener(this); 
    tv3.setOnTouchListener(this); 

    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
    // check which textview it is and do what you need to do 

    // return true if you don't want it handled by any other touch/click events after this 
    return true; 
    } 
} 
+0

は、この作業コードまたは擬似コードのですか?それがそのまま動作することはできませんでした。とにかくtks!更新されました! :) –

4

私はOnClickListenerを使用し、代わりにそれを試してみてください。

11

私はこれを使用:

 <TextView 
      android:id="@+id/txtTermsConditions" 
      android:layout_width="180dp" 
      android:layout_height="50dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="10dp" 
      android:onClick="onTermConditions" 
      android:clickable="true" 
      android:text="Terms and Conditions" 
      android:textColor="@color/blue" /> 
+1

この例では、テキストに実際のハイパーリンクが含まれていない限り、android:textColorLinkは何も行いません。 –

+0

私はあなたが正しいと思います。私は答えを編集しました。 –

3

こんにちはあなたはコードの下の両方を使用する必要があります:コードの下

<TextView 
     android:id="@+id/reg_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/login_btn" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:clickable="true" 
     android:text="Are you now need to create an account?" 
     android:textColor="#ff0000ff" 
     android:textSize="15sp" 
     /> 

そして

private TextView tv1; 
     tv1= (TextView)findViewById(R.id.reg_text); 
    tv1.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View arg0, MotionEvent arg1) { 
      // TODO Auto-generated method stub 
      ProgressDialog progressBar = ProgressDialog.show(MainActivity.this, "Title", 
        "شکیبا باشید!"); 
      progressBar.setCancelable(true); 
      Intent i = new Intent(getApplicationContext(), Register.class); 
      startActivity(i); 
      return false; 
     } 
    }); 
0

は私のために働いた:

追加to strings.xml:

<string name="about_us"><font fgcolor="#039BE5">\"THIS IS SPARTA! more info at" <font fgcolor="#000000"><a href="https://google.com/">google.com</a></font> 
</string> 

のTextViewに追加します。

android:linksClickable="true" 

は、アクティビティに追加します。

about_us.movementMethod = LinkMovementMethod.getInstance() 
+0

これはまだ@Crisが避けたいと思っていた各テキストビューの設定行為を持っています...あなたはmoveMethod(興味深い解決策) – yakobom