android textview doubleclick,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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值