簡單模擬微信長按語音發送效果

在此很少說,直接上代碼吧: java

簡單的佈局文件 android


<?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>


 

實現代碼: 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;
  }
 });
}
}
相關文章
相關標籤/搜索