![](/img/trans.png)
[英]In Android widget, how to catch the index for current view on screen in AdapterViewFlipper?
[英]How to catch clicks on widget background on android screen
我有一个4x1可调整大小的小部件。 当我单击图标时,它可以正常工作
并打开我的应用程序,但是如果我在后台单击,它什么也没做
任何想法如何使我的背景可点击? 这是我的代码:
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);
}
}
只需将click listener设置为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
在窗口小部件中,通过在视图上应用setOnClick()方法将不会像窗口小部件使用remoteviews那样简单。
要处理click事件,请执行以下操作:
例如,将id赋予您的根LinearLayout。
<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文件中:
->重写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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.