在此很少说,直接上代码吧: java
简单的布局文件 android
android:id="@+id/rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
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"
/>
android:id="@+id/click"
android:layout_width="fill_parent"
android:layout_height="80dip"
android:text="长按说话"
android:layout_alignParentBottom="true"/>
实现代码: app
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;
}
});
}
}