2011-02-14 20 views

Respuesta

12

Uso continuación Código

TextView name=((TextView)findViewById(R.id.TextView01)); 


name.hasFocus(); 
     name.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

      public void onFocusChange(View v, boolean hasFocus) { 
       // TODO Auto-generated method stub 
       if(hasFocus) 
        ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT_BOLD); 
       else 
        ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT); 

      } 
     }); 

     name.setOnTouchListener(new View.OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT_BOLD); 
       return false; 

} });

+0

Cuando doy el código de esta manera, el texto se vuelve en negrita. Pero, ¿cómo puedo eliminar mi enfoque después de hacerlo en negrita? De hecho, al hacer clic en esa vista de texto, llamo al correo electrónico. Si presiono el botón Atrás del dispositivo, este valor en negrita debería ser del estilo normal. ¿Cómo puedo hacer eso? – Mathew

+0

Hola, lo hice funcionar. Solo di una parte del código de setOnTouchListener. Mi código de intención de correo electrónico fue escrito dentro de otro método. Ahí di la fuente predeterminada. Por lo tanto, cumplió mi requisito. Gracias por la ayuda. – Mathew

1

De hecho, he creado una clase pequeña para que esto sea mucho más fácil: ClickTextView. Puedes copiarlo y pegarlo en tu proyecto y Android Studio debería arreglarlo.

public class ClickTextView extends android.support.v7.widget.AppCompatTextView { 

    Rect rect; 

    public ClickTextView(Context context) { 
     super(context); 
     init(); 
    } 

    public ClickTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public ClickTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    private void init(){ 
     setClickable(true); 
     rect = new Rect(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     int action = event.getAction(); 
     if (action == MotionEvent.ACTION_DOWN 
       || action == MotionEvent.ACTION_HOVER_ENTER){ 
      this.setTypeface(Typeface.DEFAULT_BOLD); 
      getHitRect(rect); 
     }else if (action == MotionEvent.ACTION_MOVE){ 
      boolean inView = rect.contains(getLeft() + (int) event.getX(), getTop() + (int) event.getY()); 
      this.setTypeface(inView ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); 
     }else if (action == MotionEvent.ACTION_UP 
       || action == MotionEvent.ACTION_CANCEL 
       || action == MotionEvent.ACTION_HOVER_EXIT 
       || action == MotionEvent.ACTION_OUTSIDE) { 
      this.setTypeface(Typeface.DEFAULT); 
     } 

     return super.onTouchEvent(event); 
    } 

    @Override 
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 
     super.onFocusChanged(focused, direction, previouslyFocusedRect); 

     if (focused) this.setTypeface(Typeface.DEFAULT_BOLD); 
     else this.setTypeface(Typeface.DEFAULT); 
    } 
} 

También proporciona una solución para el caso de que un usuario hace clic en su vista, pero arrastra su dedo fuera de la vista límites. Si se publica allí, Android no registrará un clic, por lo que el texto tampoco debe aparecer en negrita.

Siéntase libre de utilizarlo donde lo desee.

Cuestiones relacionadas