如何为addOnGesturePerformedListener创建单独的类

时间:2011-09-15 06:06:18

标签: java android android-ndk

这一切都是我的代码

private GestureLibrary gLib;
private static final String TAG = "com.hascode.android.gesture";
protected static final TextWatcher watcher = null;

private EditText et1;
private EditText et2, et;

// int dec = et.getText().toString().length()-1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);
    et = et1;
    gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);

    // glip1 = GestureLibraries.fromRawResource(this, R.raw.gestures1);

    if (!gLib.load()) {
        Log.w(TAG, "could not load gesture library");
        finish();
    }
    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(handleGestureListener);
}

/**
 * our gesture listener
 */
private OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() {
    @Override
    public void onGesturePerformed(GestureOverlayView gestureView,
            Gesture gesture) {

        ArrayList<Prediction> predictions = gLib.recognize(gesture);
        // ArrayList<Prediction> predictions1 = glip1.recognize(gesture);
        Toast.makeText(gestureinput.this,
                predictions.get(0).name + " " + predictions.get(0).score,
                Toast.LENGTH_SHORT).show();

        // one prediction needed
        if (predictions.size() > 0 && predictions.get(0).score > 2.0) {
            String prediction = predictions.get(0).name;
            // checking prediction

            if (prediction.equals("A")) {
                // and action
                et.append("A");

                // et.getText().insert(et.getSelectionStart(), "A");
            }
    }
    }
};

}

现在我需要为手势监听器创建单独的类(OnGesturePerformedListener)请任何人指导我如何创建这样的新类以及如何为oncreate方法调用该类请指导我我是android和java的新手.. < / p>

private OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() {
    @Override
    public void onGesturePerformed(GestureOverlayView gestureView,
            Gesture gesture) {

        ArrayList<Prediction> predictions = gLib.recognize(gesture);
        // ArrayList<Prediction> predictions1 = glip1.recognize(gesture);
        Toast.makeText(gestureinput.this,
                predictions.get(0).name + " " + predictions.get(0).score,
                Toast.LENGTH_SHORT).show();

        // one prediction needed
        if (predictions.size() > 0 && predictions.get(0).score > 2.0) {
            String prediction = predictions.get(0).name;
            // checking prediction

            if (prediction.equals("A")) {
                // and action
                et.append("A");

                // et.getText().insert(et.getSelectionStart(), "A");
            }
    }
    }
};

}

1 个答案:

答案 0 :(得分:1)

试试这种方式

class MyGestureHandler implements OnGesturePerformedListener{
    @Override
    public void onGesturePerformed(GestureOverlayView gestureView,
            Gesture gesture) {

        ArrayList<Prediction> predictions = gLib.recognize(gesture);
        // ArrayList<Prediction> predictions1 = glip1.recognize(gesture);
        Toast.makeText(gestureinput.this,
                predictions.get(0).name + " " + predictions.get(0).score,
                Toast.LENGTH_SHORT).show();

        // one prediction needed
        if (predictions.size() > 0 && predictions.get(0).score > 2.0) {
            String prediction = predictions.get(0).name;
            // checking prediction

            if (prediction.equals("A")) {
                // and action
                et.append("A");

                // et.getText().insert(et.getSelectionStart(), "A");
            }
    }     
}

并以这种方式创建对象。

MyGestureHandler myHandler = new MyGestureHandler();
gestures.addOnGesturePerformedListener(myHandler);