【问题标题】:How to perform operations with Imagebuttons widget in android如何在 android 中使用 Imagebuttons 小部件执行操作
【发布时间】:2013-06-06 06:57:50
【问题描述】:

我创建了一个简单的应用程序,其中一个活动中有 3 个图像按钮。这些按钮正在执行一些操作。此外,我想在主屏幕上创建一个具有 3 个按钮的小部件,该小部件执行与活动中的 imageButtons 相同的操作。 我已经阅读了很多教程,但找不到合适的。

小部件如下所示:

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:orientation="horizontal"
  android:background="@drawable/my_shape"
  android:layout_gravity="center"
  android:layout_height="wrap_content">

  <ImageButton
   android:id="@+id/imageButton1"
   android:layout_width="52dip"
   android:layout_height="50dip"
   android:layout_gravity="center_horizontal|center"
   android:src="@drawable/ic_launcher" />

  <ImageButton
   android:id="@+id/imageButton2"
   android:layout_width="52dip"
   android:layout_height="50dip"
   android:layout_gravity="center_horizontal|center"
   android:src="@drawable/ic_launcher" />

  <ImageButton
   android:id="@+id/imageButton3"
   android:layout_width="52dip"
   android:layout_height="50dip"
   android:layout_gravity="center_horizontal|center"
   android:src="@drawable/ic_launcher" />

  <ImageButton
   android:id="@+id/imageButton3"
   android:layout_width="52dip"
   android:layout_height="50dip"
   android:layout_gravity="center_horizontal|center"
   android:src="@drawable/ic_launcher" />

   </LinearLayout>

在 res/xml 中的Widget_info.xml:

 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
 android:minWidth="146dip"
 android:minHeight="72dip"
 android:updatePeriodMillis="0"
 android:initialLayout="@layout/widget_layout"/>

我在 manifestfile 中也有小部件的详细信息。

我没有在此处包含活动代码。在活动中,有 3 个 imageButton onclick 方法,我希望小部件按钮(3 个按钮)完全执行这些操作。

请任何人都可以指导我。提前致谢

【问题讨论】:

    标签: android android-widget imagebutton


    【解决方案1】:

    您可以在setOnClickPendingIntent 的帮助下实现这一目标。

    public class Widget extends AppWidgetProvider {
    
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                int[] appWidgetIds) {
            super.onUpdate(context, appWidgetManager, appWidgetIds);
            final int N = appWidgetIds.length;
            for (int i = 0; i < N; i++) {
                int appWidgetId = appWidgetIds[i];
                        // For first image
                Intent intent = new Intent(context, SplashActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                        intent, 0);
                RemoteViews views = new RemoteViews(context.getPackageName(),
                        R.layout.appwidget);
                views.setOnClickPendingIntent(R.id.imageButton1, pendingIntent);
                        //For second Image
                Intent intent1 = new Intent(context, HomeActivity.class);
                PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0,
                        intent1, 0);
                views.setOnClickPendingIntent(R.id.imageButton2, pendingIntent1);
    
                        // like wise you can add your consecutive classes.
    
                appWidgetManager.updateAppWidget(appWidgetId, views);
            }
    
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 你能说得更清楚吗..小部件按钮上的 onclick 将与活动中按钮的 onclick 行为相同
    • 请注意views.setOnClickPendingIntent(R.id.imageButton1, pendingIntent1);。在这一行中,您必须通过单击相应的imageButton 来定义必须调用哪个pendingIntent
    • 对不起,帮我看看我的代码,活动代码在上面,请检查
    【解决方案2】:

    在我的 HomeActivity 中:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
        imgbtn1=(ImageButton)findViewById(R.id.imageCallButton1);
        imgbtn2=(ImageButton)findViewById(R.id.imageFBButton1);
        imgbtn3=(ImageButton)findViewById(R.id.imageSmsButton1);
    
        imgbtn1.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "U clicked on Call Button", Toast.LENGTH_LONG).show();
            }
        });
      imgbtn2.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "U clicked on FB Button", Toast.LENGTH_LONG).show();
            }
        });
       imgbtn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "U clicked on SMS Button", Toast.LENGTH_LONG).show();
        }
    });
        }
    

    在我的 MyWidgetProvider 活动中:

    public class MyWidgetProvider extends AppWidgetProvider {
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
           //what should be added here
        super.onUpdate(context, appWidgetManager, appWidgetIds);
      }
             }
    

    【讨论】:

    • 没有人。 AppWidgetProvider 用于在小部件的帮助下从主屏幕启动活动(一些视图。在您的情况下为图像按钮。)。因此,您必须通过在MyWidgetProvider 中单击小部件来定义必须调用的活动。您的活动将不受任何干扰地进行。您只需要更改 MyWidgetProvider 课程(就像我的回答一样)。或者只是谷歌关于“小部件和活动之间的区别”。
    • 完美..!得到了我想要的东西。非常感谢
    • 另外,如果我不想调用整个活动,我只想通过此按钮小部件执行活动内的按钮 onclick 事件
    • 为此,请查看here。完美的教程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多