我在我的应用程序上实现了手势检测,它在活动中运行良好。
现在,我想要做的是能够在AlertDialog中使用这些手势检测。 事实上我有一个带有取消按钮的活动。当我点击这个按钮时,用户必须在AlertDialog中确认他的选择,它有两个按钮:是和否。但是,在这个AlertDialog中,滑动手势不起作用,我无法在是或否之间做出选择,因为它只关注No按钮。
我想为此警报对话框实施滑动手势检测。这是我做的:
package com.example.recente;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;
public class ChoixUtilisateur extends Activity {
private GestureDetector mGestureDetector;
Button cancelno;
Button cancelyes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGestureDetector = createGestureDetector(this);
setContentView(R.layout.fragment_choix_utilisateur);
Button execute = (Button)findViewById(R.id.execute);
execute.setFocusable(true);
execute.setFocusableInTouchMode(true);///add this line
execute.requestFocus();
}
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
//Create a base listener for generic gestures
gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
Button cancel = (Button)findViewById(R.id.cancel);
AlertDialog.Builder cancelbutton = new AlertDialog.Builder(ChoixUtilisateur.this);
if (gesture == Gesture.TAP) {
// do something on tap
if (cancel.hasFocus()){
// .getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)
cancelbutton.setTitle("Confirm");
cancelbutton.setMessage("Do you really want to proceed ?");
cancelbutton.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int i){
dialog.cancel();
Intent intent = new Intent(ChoixUtilisateur.this,Cancel.class);
startActivity(intent);
finish();
}
});
cancelbutton.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int i){
dialog.cancel();
}
});
cancelbutton.setCancelable(false) ;
cancelbutton.create();
AlertDialog dialog = cancelbutton.create();
cancelno = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
cancelyes = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
cancelbutton.show();
}
else{
}
return true;
} else if (gesture == Gesture.TWO_TAP) {
// do something on two finger tap
return true;
} else if (gesture == Gesture.SWIPE_RIGHT) {
// do something on right (forward) swipe
if(cancelno.hasFocus()){
cancelyes.setFocusable(true);
cancelyes.setFocusableInTouchMode(true);///add this line
cancelyes.requestFocus();
}
else{
cancelno.setFocusable(true);
cancelno.setFocusableInTouchMode(true);///add this line
cancelno.requestFocus();
}
return true;
} else if (gesture == Gesture.SWIPE_LEFT) {
// do something on left (backwards) swipe
if(cancelno.hasFocus()){
cancelyes.setFocusable(true);
cancelyes.setFocusableInTouchMode(true);///add this line
cancelyes.requestFocus();
}
else{
cancelno.setFocusable(true);
cancelno.setFocusableInTouchMode(true);///add this line
cancelno.requestFocus();
}
return true;
}
return false;
}
});
return gestureDetector;
}
}
答案 0 :(得分:3)
我不确定使用警告对话框是Google Glass的最佳做法,使用标准卡,或者如果您想要更多自定义,则可以使用简单的活动。