2012-08-10 216 views
0

我有一点问题。我有它旁边的textview按钮。我想在触摸文本视图时触摸按钮(效果:当我触摸按钮时突出显示按钮)。有没有一些简单的方法来做到这一点?我找不到任何适当的功能。当我触摸TextView时触摸按钮

编辑:好的,我解决了我的问题。它是:

hilfeText.setOnTouchListener(新OnTouchListener(){

 public boolean onTouch(View v, MotionEvent me) { 
      int action = me.getAction(); 
      if(action == MotionEvent.ACTION_DOWN) { 
       hilfe.setPressed(true); 
       return true; 
      } else if (action == MotionEvent.ACTION_UP) { 
       hilfe.setPressed(false); 
       return true; 
      } 
      return false; 
     } 
    }); 

回答

1

可以在xml文件 定义button的点击对焦模式类似 `

<item android:drawable="@drawable/recordings_icon" android:state_enabled="false"></item> 
<item android:drawable="@drawable/recordings_glow" android:state_enabled="true" android:state_pressed="true"/> 
<item android:drawable="@drawable/recordings_shadow" android:state_enabled="true" android:state_focused="true"/> 
<item android:drawable="@drawable/recordings_icon" android:state_enabled="true"/> 

你可以把这个文件在目录名称中drawables @drawable/recordings_icon是图像文件 你只需要声明这个文件名作为背景button布局xml文件 <Button android:id="@+id/buttonActivate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/xmlfilename" />

0

设置一个onClickListenerButtonTextView并在onClick(),让他们都调用同一个功能简单

+0

我没有问单击按钮。我要求接触它。点击没有任何持续时间,所以它不会突出显示我的按钮。 – mglisty 2012-08-10 09:18:25

+0

只需setonTouchListener为button和textview,并在onTouch()函数中实现相同。但我不建议这样做,因为3次触摸将被称为 - 一个用于动作上,下,和移动 – pixelscreen 2012-08-10 09:34:38

1

。您可以使用Button reference内的TextViewonClick调用performClick()方法。

示例 -

myTextView.setOnClickListner(){ 

    public void onClick(){ 
     myButton.performClick(); 

     --------------code 

    } 
} 
0

这可能做的伎俩

TextView v = ...; 
final Button b = ...; 
v.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     b.setFocusableInTouchMode(true); 
     b.requestFocus(); 
     b.performClick(); 
    } 
});