开发者社区> 问答> 正文

如何在小部件Android中动态添加按钮

我正在用Java开发android应用程序,我必须在小部件中创建一些按钮,这些按钮是动态添加的。我尝试了很多解决方案,但是它们无法正常工作,因此请不要将此问题标记为重复。这是我的Widget.java:


package com.fxteam.malcome;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Button;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import android.util.Log;

public class Widget extends AppWidgetProvider {

    private static final String SYNC_CLICKED    = "malcome_widget_button";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {


        for (int appWidgetId : appWidgetIds) {
            Intent intent2 = new Intent(context, MainActivity.class);
            intent2.putExtra("accountID", "0");
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_app_widget); 
            views.setOnClickPendingIntent(R.id.malcome_widget_button, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }



        /*Button myButton = new Button(context);
        myButton.setText("Push Me");

        LinearLayout ll = (LinearLayout) findViewById(R.id.widget_layout); //<- this give me error "cannot find symbol method findViewById(int)"
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);*/
    }
}

展开
收起
Puppet 2020-01-15 11:57:32 571 0
1 条回答
写回答
取消 提交回答
  • 您可以使用:

    
    private Button button;
    private LinearLayout linearLayout;
    
    private final int USERID = 6000;
    private int countID;
    
    button = (Button) findViewById(R.id.button);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button b = new Button(getApplicationContext());
                b.setText("text on btn " + Integer.toString(countID + 1));
                b.setLayoutParams(
                        new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT)
                );
                b.setId(USERID + countID);
                b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // to do
                    }
                });
                linearLayout.addView(b);
                countID++;
            }
        });
    
    2020-01-15 11:57:49
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载