Android的TextView的双击事件监听

TextView是没有双击事件的监听设置的,故我们这里使用的是setOnTouchListener来作为双击监听。

监听的类需要实现OnTouchListener类,并重写OnTouch方法

下面是实例:

public class HosIntroActivity extends MyActivity implements OnTouchListener {
	private static long size = 12; //字体大小
	private int count = 0;
	private int firClick = 0;
	private int secClick = 0;
	private int flage = 0;
	private TextView infor;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_introduction);
		infor = (TextView) findViewById(R.id.text_infor);

		try {
			infor.setText(Reader.readText(this));
		} catch (Exception e) {
			Toast.makeText(this, "读取文件失败", Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
		// float zoomScale = 0.5f;// 缩放比例
		// new ZoomTextView(infor, zoomScale);
		infor.setOnTouchListener(this);
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (MotionEvent.ACTION_DOWN == event.getAction()) {
			count++;
			if (count == 1) {
				firClick = (int) System.currentTimeMillis();
			} else if (count == 2) {
				secClick = (int) System.currentTimeMillis();
				switch (flage) {
				case 0:
					if (secClick - firClick < 1000) {// 双击事件
						size = size + 5;
						infor.setTextSize(size);
						flage++;
					}
					count = 0;
					firClick = 0;
					secClick = 0;
					return true;
				case 1:
					if (secClick - firClick < 1000) {// 双击事件
						size = size - 5;
						infor.setTextSize(size);
						flage--;
					}
					count = 0;
					firClick = 0;
					secClick = 0;
					return true;
				default:
					return true;
				}
			}
		}

		return true;
	}

}

参考: http://abc20899.iteye.com/blog/1068432

参考中其实给出了两种方式

public class MainActivity extends Activity{ //implements OnGestureListener{ 

  private Button button; 

           @Override  
  public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main); 
             button = (Button)findViewById(R.id.btn_hello); 
    button.setOnTouchListener(new onDoubleClick()); 
  } 
  
  class onDoubleClick implements View.OnTouchListener{   
int count = 0; 
int firClick = 0; 
int secClick = 0; 
    @Override  
    public boolean onTouch(View v, MotionEvent event) {   
        if(MotionEvent.ACTION_DOWN == event.getAction()){   
            count++;   
            if(count == 1){   
                firClick = (int) System.currentTimeMillis();   
                   
            } else if (count == 2){   
                secClick = (int) System.currentTimeMillis();   
                if(secClick - firClick < 1000){   
                       //双击事件 
                button.setText("wff"); 
                }   
                count = 0;   
                firClick = 0;   
                secClick = 0;   
            }   
        }   
        return true;   
    }   
} 
}


你可能感兴趣的:(android,UI,android,button,exception,class,float)