【问题标题】:Use widget as button - android eclipse使用小部件作为按钮 - android eclipse
【发布时间】:2013-09-28 04:09:47
【问题描述】:

我有一个小部件,我想在用户单击小部件时打开一个新窗口。 有人能告诉我怎么做吗,你能给我一些代码吗? 我目前正在学习如何使用 java 在 Eclipse 中制作 android 应用程序。 提前致谢!

【问题讨论】:

  • 正确搜索,你会得到很多教程。

标签: android eclipse button widget


【解决方案1】:

您可以通过添加使任何视图可点击

android:clickable="true"

到 xml 或通过调用

view.setClickable(true);

在java中。 见documentation

然后您可以分配一个on click listener 来执行您想要的任何操作。

【讨论】:

    【解决方案2】:

    更改扩展 AppWidgetProvider 的类。您可以看到 basic example 用于创建简单的小部件。因此您可以在扩展 AppWidgetProvider 的类中检测小部件的点击事件。在这里,MyWidgetProvider 是一个将在后台运行的服务。

    public class MyWidgetProvider extends AppWidgetProvider {
    
            @Override
            public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                    int[] appWidgetIds) {
    
                RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetview);
                remoteViews.setOnClickPendingIntent(R.id.widgetlayout1, buildButtonPendingIntent(context));
            pushWidgetUpdate(context, remoteViews);
            }
    
        public static PendingIntent buildButtonPendingIntent(Context context) {
    
          Log.d("MyWidgetProvider", "click");
         Intent intent = new Intent(context,secondactivity.class);// Add secondactivity to Manifest
         context.startActivity(intent);
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            }
    

    我已经看到你正在使用的link。你可以将上面的代码放在你的LovelyBatteryWidget 类中。

    编辑

    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
            ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(context);
            manager.updateAppWidget(myWidget, remoteViews);     
        }
    

    查看this教程供您参考。

    【讨论】:

    • 我有 1 个错误:pushWidgetUpdate(context, remoteViews); //找不到方法
    • 如果我创建一个新的 pushWidgetUpdate() 方法,我应该在方法中放置什么?
    • 我应该将带有类声明的整个代码放在 LovelyBatteryWidget 类中吗?我应该在 pushWidgetUpdate 方法中放置什么,我必须创建,所以我没有得到找不到方法错误?提前致谢!
    • 我在看教程,他们使用了一个小部件按钮,我还看了新的波士顿教程,他们也使用了一个按钮,所以我的问题是:如何将小部件转换为按钮,所以我可以在那个按钮上使用监听器?请查看我正在使用的网站上的代码。这是我需要使用此按钮的行:remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context)); //这段代码不是widget_button,而是来自你给我的教程链接
    • 有没有办法可以将用作小部件的可绘制对象添加到按钮?
    【解决方案3】:

    在我的代码中为新窗口创建另一个活动 seconhdActivity

    在小部件的 onclickevent 中使用我在代码中使用的 Intent

    Intent mIntent = new Intent(MainActivity.this,
                        SecondActivity.class);
                startActivity(mIntent);
    

    ================================================ =============================

    公共类 MainActivity 扩展 Activity {

    Button btnButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        btnButton = (Button) findViewById(R.id.button1);
        btnButton.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                Intent mIntent = new Intent(MainActivity.this,
                        SecondActivity.class);
                startActivity(mIntent);
    
            }
        });
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      相关资源
      最近更新 更多