如何捕捉在Android屏幕上的小部件背景的点击

问题描述:

我有一个4x1可调整大小的小部件。当我点击图标时,它工作正常
并打开我的应用程序,但如果我点击背景,它什么都不做
任何想法如何使我的背景点击? description 这里是我的代码:如何捕捉在Android屏幕上的小部件背景的点击

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example"> 
    <application> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
      </intent-filter> 
      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/myWidgetXml"/> 
     </receiver> 
    </application> 
</manifest> 

myWidgetXml.xml

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialKeyguardLayout="@layout/myLayout" 
    android:initialLayout="@layout/myLayout" 
    android:minHeight="40dp" 
    android:minWidth="40dp" 
    android:resizeMode="horizontal" 
    android:updatePeriodMillis="86400000" 
    android:widgetCategory="home_screen"> 
</appwidget-provider> 

myLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/widget_margin" 
    android:layout_margin="0dp" 
    android:gravity="center" 
    android:background="@android:drawable/alert_dark_frame" 
    android:orientation="vertical"> 

    <ImageButton 
     android:id="@+id/WidgetButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:src="@drawable/widget_pic" /> 
    <TextView 
     android:id="@+id/WidgetTextView" 
     android:textColor="@android:color/white" 
     android:textSize="14sp" 
     android:layout_marginTop="6dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" /> 
</LinearLayout> 

MyWidget.java

public class MyWidget extends AppWidgetProvider { 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     for (int appWidgetId : appWidgetIds) 
      updateAppWidget(context, appWidgetManager, appWidgetId); 
    } 

    private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) { 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout); 
     Intent intent = new Intent(context, MainActivity.class); 
     Uri.Builder builder = new Uri.Builder(); 
     intent.setData(builder.build()); 
     intent.setAction(Intent.ACTION_VIEW); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent); 
     views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic); 
     views.setTextViewText(R.id.WidgetTextView, "text"); 
     _appWidgetManager.updateAppWidget(WidgetId, views); 
    } 
} 
+0

尝试这一个'机器人:'线性布局'部分中的'myLayout.xml'中的clickable =“true”''如果它不起作用尝试将'background'拷贝到'src' – Mikhail

+1

@Mikhail,它没有工作,但是感谢你的努力: - ) –

只需设置点击收听到rootview,不要的ImageButton。

现在您有:

views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent); 

给id来根的LinearLayout例如。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/rootView" 

这里

views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent); 

views.setOnClickPendingIntent(R.id.rootView, pendingIntent); 

R.id.WidgetButton更改为R.id.rootView

+1

非常感谢,很好的回答:) –

+0

btw当我改变它backgrou nd点击开始工作,但点击图标没有,所以我只是设置这两个听众甚至tho它似乎很奇怪 –

+1

只需添加到LinearLayout属性android:descendantFocusability =“blocksDescendants” 和ImageButton clickable = false,focusable = false –

窗口小部件点击不会通过的意见,小部件应用setOnClick()方法的工作一样简单使用远程视图。

对于处理click事件做以下操作:

  1. 在你myLayout.xml添加id来是 的LinearLayout的Android父布局:ID = “@ + ID/lnr_widget_back”
  2. 在你AndroidManifest。 xml文件:

例如给你的根的LinearLayout id。

<receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" > 
<intent-filter> 
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"> 
</action> 
</intent-filter> 
<meta-data android:name="android.appwidget.provider" android:resource="@xml/myWidgetXml"/> 
</receiver> 

再添加一个动作你的意图过滤器象下面这样:

<action android:name="CLICK.MY_APP.WIDGET" ></action> 

注意:您可以给任何名义到这一行动,但要确保是 自己独特的方式在里面。如果它不是唯一的,您可能无法通过 接收点击事件/ 3.在您的MyWidget中。Java文件:

- > overrride的onReceive()方法:

@Override public void onReceive(Context context, Intent intent) { 
if(intent.getAction().equals("CLICK.MY_APP.WIDGET")) { // Your view 
is clicked // Code for after click } super.onReceive(this.context, 
intent); } 

- >在updateAppWidget()方法来设置挂起的意图,将处理您点击:

Intent intent = new Intent("CLICK.MY_APP.WIDGET"); PendingIntent 
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
PendingIntent.FLAG_UPDATE_CURRENT); 
views.setOnClickPendingIntent(R.id.lnr_widget_back, pendingIntent);