我尝试在onTouch方法中使用MotionEvent但是......它永远不会起作用
我还在google上搜索过,发现在onTouch方法中它需要返回true。
我做了但仍然不行......
任何想法?...
imageViewCancel.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Log.d(TAG, "ACTION : " + event.getAction());
switch(event.getAction()){
case MotionEvent.ACTION_UP:
Log.d(TAG, "ACTION UP");
view.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "ACTION DOWN");
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
break;
default:
break;
}
return true;
}
});
这是我的xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#85d2ce">
<LinearLayout
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout">
<ImageView
android:id="@+id/imageViewWakeUp"
android:layout_width="140.5dp"
android:layout_height="121.5dp"
android:src="@drawable/messages"
android:layout_gravity="center_horizontal"/>
<ImageView
android:id="@+id/imageViewAlarmClock"
android:layout_width="157.5dp"
android:layout_height="153.5dp"
android:src="@drawable/ic_alarm"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
/>
<TextView
android:id="@+id/textViewAmPm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#fff"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
fontPath="fonts/Roboto-Bold.ttf"
/>
<TextView
android:id="@+id/textViewRingingTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="44dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textStyle="bold"
fontPath="fonts/Roboto-Bold.ttf"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout_cancelWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:weightSum="1"
android:layout_below="@+id/linearLayout"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/linearLayoutLeftArrowWrapper"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="0.21">
<ImageView
android:id="@+id/imageViewLeftArrow"
android:layout_width="47dp"
android:layout_height="24.5dp"
android:layout_gravity="center_vertical"
android:src="@drawable/arrow_left"
/>
</LinearLayout>
<ImageView
android:id="@+id/imageViewSnooze"
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_weight="0.17"
android:clickable="true"
android:src="@drawable/ic_alarm_10min"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.22">
</LinearLayout>
<ImageView
android:id="@+id/imageViewCancel"
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_weight="0.17"
android:clickable="true"
android:src="@drawable/ic_alarm_off"/>
<LinearLayout
android:id="@+id/linearLayoutRightArrowWrapper"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="0.21">
<ImageView
android:id="@+id/imageViewRightArrow"
android:layout_width="47dp"
android:layout_height="23dp"
android:layout_gravity="center_vertical"
android:src="@drawable/arrow_right"/>
</LinearLayout>
</LinearLayout>
这是我的Activity java代码
public class AlarmAlertLvOneActivity extends AppCompatActivity {
private static final String TAG = "AlarmAlertLvOneActivity";
private Alarm alarm;
private SharedPreferences sharedPreferences;
private AlarmLogDataBaseManager alarmLogDataBaseManager;
private SnoozeDataBaseManager snoozeDataBaseManager;
private int time;
private long timeInMillis;
private AlarmAlert alarmAlert;
@Override
public void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_alert_lv_one);
Bundle bundle = getIntent().getExtras();
alarm = bundle.getParcelable("Alarm");
sharedPreferences = getSharedPreferences("app_setting", MODE_PRIVATE);
alarmLogDataBaseManager = new AlarmLogDataBaseManager(getApplicationContext(), null, null, -1);
snoozeDataBaseManager = new SnoozeDataBaseManager(getApplicationContext(), null, null, -1);
if (getIntent().getStringExtra("mode") != null && getIntent().getStringExtra("mode").equals("snooze")) {
snoozeDataBaseManager.snoozeOff(getIntent().getLongExtra("snoozeId", 0));
AppController.getInstance().setNotificationController(getApplicationContext());
AppController.getInstance().getNotificationController().notifyNotificationChanged(NotificationController
.NOTI_SNOOZE);
}
alarmAlert = new AlarmAlert(getApplicationContext(), alarm);
time = 60 * Calendar.getInstance().get(Calendar.MINUTE) + 60 * 60 * Calendar.getInstance().get(Calendar
.HOUR_OF_DAY);
timeInMillis = Calendar.getInstance().getTimeInMillis();
setArrowAnimation();
initViews();
initSoundAndVibrate();
}
@Override
protected void onResume() {
super.onResume();
}
Animation fadeInOutAnimcation;
private void setArrowAnimation() {
fadeInOutAnimcation = new AlphaAnimation(0, 1);
fadeInOutAnimcation.setInterpolator(new LinearInterpolator());
fadeInOutAnimcation.setDuration(1000);
fadeInOutAnimcation.setRepeatMode(Animation.REVERSE);
fadeInOutAnimcation.setRepeatCount(Animation.INFINITE);
}
ImageView imageViewCancel, imageViewSnooze, imageViewRightArrow, imageViewLeftArrow;
LinearLayout linearLayoutRightArrowWrapper, linearLayoutLeftArrowWrapper;
TextView textViewRingingClock, textViewAmPm;
private void initViews() {
setTextViewAmPm();
setTextViewRingingClock();
linearLayoutLeftArrowWrapper = (LinearLayout) findViewById(R.id.linearLayoutLeftArrowWrapper);
linearLayoutLeftArrowWrapper.setAnimation(fadeInOutAnimcation);
imageViewLeftArrow = (ImageView) findViewById(R.id.imageViewLeftArrow);
imageViewSnooze = (ImageView) findViewById(R.id.imageViewSnooze);
imageViewSnooze.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
return false;
}
});
linearLayoutRightArrowWrapper = (LinearLayout) findViewById(R.id.linearLayoutRightArrowWrapper);
linearLayoutRightArrowWrapper.setAnimation(fadeInOutAnimcation);
imageViewRightArrow = (ImageView) findViewById(R.id.imageViewRightArrow);
imageViewRightArrow.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch(event.getAction()){
case DragEvent.ACTION_DROP:
Log.d(TAG, "Arrow ACTION DROP");
finish();
break;
default:
break;
}
return true;
}
});
imageViewCancel = (ImageView) findViewById(R.id.imageViewCancel);
imageViewCancel.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Log.d(TAG, "ACTION : " + event.getAction());
switch(event.getAction()){
case MotionEvent.ACTION_UP:
Log.d(TAG, "ACTION UP");
//view.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "ACTION CANCEL");
//view.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "ACTION DOWN");
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
if(android.os.Build.VERSION.SDK_INT >= 24)
view.startDragAndDrop(data, shadowBuilder,view,0);
else
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
break;
default:
break;
}
return true;
}
});
}
private void setTextViewAmPm(){
textViewAmPm = (TextView) findViewById(R.id.textViewAmPm);
if ((Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) > 12)
textViewAmPm.setText("PM");
else
textViewAmPm.setText("AM");
}
private void setTextViewRingingClock(){
textViewRingingClock = (TextView) findViewById(R.id.textViewRingingTime);
if (sharedPreferences.getBoolean("24Hour_OnOff", false)) {
if ((Calendar.getInstance().get(Calendar.MINUTE)) < 10)
textViewRingingClock.setText((Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) + ":" + "0" +
(Calendar.getInstance().get(Calendar.MINUTE)));
else
textViewRingingClock.setText((Calendar.getInstance().get(Calendar.HOUR_OF_DAY)) + ":" + (Calendar
.getInstance().get(Calendar.MINUTE)));
} else {
if ((Calendar.getInstance().get(Calendar.MINUTE)) < 10)
textViewRingingClock.setText((Calendar.getInstance().get(Calendar.HOUR)) + ":" + "0" + (Calendar
.getInstance().get(Calendar.MINUTE)));
else
textViewRingingClock.setText((Calendar.getInstance().get(Calendar.HOUR)) + ":" + (Calendar
.getInstance().get(Calendar.MINUTE)));
}
}
private void initSoundAndVibrate() {
alarmAlert.initSoundAndVibrate(alarm.isVibrationIsOn(), alarm.getSoundUriInString());
}
protected void onDestroy() {
super.onDestroy();
alarmAlert.quitSonndAndVibrate();
alarmLogDataBaseManager.saveLogData(
Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH),
time,
alarm.getTime(),
(int) (Calendar.getInstance().getTimeInMillis() - timeInMillis) / 1000,
true);
Log.e("Elapsed", (Calendar.getInstance().getTimeInMillis() - timeInMillis) / 1000 + "");
AppController.getInstance().setNotificationController(getApplicationContext());
AppController.getInstance().getNotificationController().notifyNotificationChanged(NotificationController
.NOTI_ALARM);
AppController.getInstance().getNotificationController().notifyNotificationChanged(NotificationController
.NOTI_SNOOZE);
}
}