2012-11-27 19 views
3

點擊我主持我的應用程序內的部件和需要知道什麼時候用戶點擊一個窗口小部件,或當窗口小部件配置意圖開始(與配置部件)認識上配置的部件

OnUserLeaveHint是不是一種選擇。

回答

1

用一些佈局包裝您的小部件並覆蓋onInterceptTouchEvent方法。

2

我已經實現了你正在努力實現的目標。解決方案的想法是使用捆綁參數is_clicked = true將OnClick待定意圖設置爲小部件。

這是你可以做什麼:

在您使用RemoteViews設置你的小部件的佈局相同的地方,做了下:

/* 
* Create pending intent to configuration activity 
*/ 
Intent intent = new Intent(context, ConfigurationMainActivity.class); 

/* 
* Add values with this intent: widget id, and is_clicked = true 
*/ 
Bundle extra = new Bundle(); 
extra.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
extra.putBoolean(ConfigurationMainActivity.IS_ON_WIDGET_CLICK_KEY, true); 
intent.putExtras(extra); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

/* 
* Set this intent to one of the views in the widget 
*/ 
remoteViews.setOnClickPendingIntent(R.id.widget_main, pendingIntent); 

2. 當用戶點擊小部件時,將打開ConfigurationMainActivity活動。 在本次活動做了下編碼:

public static final String IS_ON_WIDGET_CLICK_KEY = "IS_ON_WIDGET_CLICK_KEY"; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    // some usual stuff 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_configuration); 

    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 

    // get the widget id that was transferred from on click event 
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 

    // ---> here is what you asking for ---> 
    // check whereas the widget was clicked by the user or not 
    boolean isOnWidgetClick = extras.getBoolean(IS_ON_WIDGET_CLICK_KEY, false); 

    if (isOnWidgetClick) 
    { 
     // ----- Do here whatever you want ------ 
    } 
    else 
    { 
     // the code of first time widget configuration 
    } 

    ... 
    ... 
} 

注:

  • IS_ON_WIDGET_CLICK_KEY - 僅僅是在多個類使用的常量。你可以在遠程視圖設置中看到它,這裏

希望,我可以幫助你。

+0

什麼是遠程視圖? –

+0

我用更簡單的方法解決了它,只是用我的小部件容器攔截點擊。 –