2013-04-09 82 views
0

好吧,繼承人的問題。我已經制作了一個小部件,現在需要檢測輕拍並雙擊它,當觸發時應顯示不同的視圖。出於測試目的,我使用了日誌記錄來查看它是否正在工作。下面是我的代碼:Android Widget檢測水龍頭和雙擊

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.GestureDetector.OnDoubleTapListener; 
import android.view.GestureDetector.OnGestureListener; 
import android.view.MotionEvent; 
import android.widget.Toast; 

/** 
* @Author Jan Zivkovic 
* 
* NE DELA, OVERRIDA AMPAK NE DELA! 
*/ 

public class UpdateWidget extends Activity implements OnGestureListener{ 

    private GestureDetector gesture; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 
     Log.w("TEST", "started"); 


     gesture = new GestureDetector(this); 

     //set the on Double tap listener 
     gesture.setOnDoubleTapListener(new OnDoubleTapListener() 
     { 
      @Override 
      public boolean onDoubleTap(MotionEvent e) 
      { 
       //set text color to green 
       //tvTap.setTextColor(0xff00ff00); 
       //print a confirmation message 
       //tvTap.setText("The screen has been double tapped."); 
       Log.w("TEST", "Double tap"); 
       return false; 
      } 

      @Override 
      public boolean onDoubleTapEvent(MotionEvent e) 
      { 
       //if the second tap hadn't been released and it's being moved 
       if(e.getAction() == MotionEvent.ACTION_MOVE) 
       { 
        //set text to blue 
        //tvTapEvent.setTextColor(0xff0000ff); 
        //print a confirmation message and the position 
        //tvTapEvent.setText("Double tap with movement. Position:\n" 
        //  + "X:" + Float.toString(e.getRawX()) + 
        //  "\nY: " + Float.toString(e.getRawY())); 
       } 
       else if(e.getAction() == MotionEvent.ACTION_UP)//user released the screen 
       { 
        //setContentView(); 
       } 
       return false; 
      } 

      @Override 
      public boolean onSingleTapConfirmed(MotionEvent e) 
      { 
       //set text color to red 
       //tvTap.setTextColor(0xffff0000); 
       //print a confirmation message and the tap position 
       //tvTap.setText("Double tap failed. Please try again."); 
       Log.w("TEST", "Single tap"); 
       return false; 
      } 
     }); 
    } 

    @Override 
    public boolean onDown(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, 
      float arg3) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, 
      float arg3) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

控件提供者類的onUpdate方法(觸發每5分鐘)

@Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     for (int i = 0; i < appWidgetIds.length; i++) { 
      int appWidgetId = appWidgetIds[i]; 
      Intent updateIntent = new Intent(context, UpdateWidget.class); 
      // Identifies the particular widget... 
      updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
      updateIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      // Make the pending intent unique... 
      updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME))); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, updateIntent, 0); 


      //context.startActivity(updateIntent); 

      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);//R.layout.widget_layout 
      views.setTextViewText(R.id.subject1, "subject1 test"); 
      views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent); 
      appWidgetManager.updateAppWidget(appWidgetId, views); 
      Log.w("Schedule", "Widget Updated = " + appWidgetId); 
     } 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 
    } 

但繼承人它變得如此遙遠得。通常當你點擊或雙擊一個部件時,手機會振動。使用此功能時,手機在敲擊時不會振動,也不會在logcat中打印任何內容。這個問題在過去幾天令我很煩惱。希望別人會知道我做錯了什麼。

編輯:忘了最重要的事情。使用在三星Galaxy王牌Android 2.2的API級別8(升級Froyo)和測試IM VE採用Android 2.3.6

+0

你不是在觸摸你的'GestureDetector'的onTouch()事件? – Graeme 2013-04-09 16:11:53

+0

你能告訴我怎麼樣? – n00b 2013-04-09 16:14:52

回答

1

我做了一個widget

鑑於你的代碼,我會假設你意味着其他Android開發人員稱之爲「應用小部件」。

,現在需要檢測它

雙水龍頭水龍頭,雙擊將是難以察覺。

下面是我的代碼

你的代碼的第一個塊是一個活動,這是不是一個應用程序部件。

當用戶點擊應用程序窗口小部件時,您的第二個代碼塊會調用一個活動。這很好,但這不會幫助您檢測到雙擊。事實上,它通常會阻止用戶首先進行雙擊,因爲活動將在第一次點擊後接管屏幕。

我會勸你避免嘗試使用應用小部件的雙擊。在應用程序小部件中有兩個不同的項目來區分不同的事件(無論您想要單擊一次,還是想要雙擊都可以)。