您的位置:首页 > 移动开发 > 微信开发

简单模拟微信长按语音发送效果

2013-07-26 10:25 253 查看
在此不多说,直接上代码吧:

简单的布局文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/rl"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffffff">

<TextView

android:id="@+id/tv"

android:layout_width="fill_parent"

android:layout_height="80dip"

android:layout_marginTop="30dip"

android:text="松开取消语音发送"

android:gravity="center"

android:textSize="20sp"

android:textColor="@android:color/black"

android:visibility="gone"

/>

<Button

android:id="@+id/click"

android:layout_width="fill_parent"

android:layout_height="80dip"

android:text="长按说话"

android:layout_alignParentBottom="true"/>

</RelativeLayout>

实现代码:

import android.R.integer;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;

public class ClickSendActivity extends Activity {

private int height;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.click);

View click=findViewById(R.id.click);

final TextView tv=(TextView) findViewById(R.id.tv);

click.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

tv.setVisibility(View.VISIBLE);

tv.setText("手指上滑取消发送");

break;

case MotionEvent.ACTION_MOVE:

if(event.getY()<0){

tv.setText("手指松开取消发送");

}else{

tv.setText("手指上滑取消发送");

}

break;

case MotionEvent.ACTION_UP:

if(event.getY()<0){//因为getY是相对控件本身的坐标,所以当<0时,手指已不再此控件上

Toast.makeText(ClickSendActivity.this, "取消发送", 1).show();

}else {

Toast.makeText(ClickSendActivity.this, "正在发送", 1).show();

}

break;

default:

break;

}

return true;

}

});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: