【问题标题】:Temporarily disable android widget button暂时禁用 android 小部件按钮
【发布时间】:2017-02-19 21:32:33
【问题描述】:

我有一个由两个按钮和一个图像组成的小部件。我想让它在用户按下按钮时禁用 30 秒。怎样才能达到这种效果?这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin"
    android:background="#55000000">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@drawable/bulb"/>

    <Button
        android:id="@+id/onButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="On"
        android:background="@drawable/appwidget_dark_bg_clickable"
        />

    <Button
        android:id="@+id/offButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Off"
        android:background="@drawable/appwidget_dark_bg_clickable"/>

</LinearLayout>

这是我当前的控制器:

public class WidgetProvider extends AppWidgetProvider {
    public static String ACTION_WIDGET_ON = "Lights turning on";
    public static String ACTION_WIDGET_OFF = "Lights turning off";
    private String serverUrl = //put your server URL here

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        Intent active = new Intent(context, WidgetProvider.class);
        active.setAction(ACTION_WIDGET_ON);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.onButton, actionPendingIntent);

        active = new Intent(context, WidgetProvider.class);
        active.setAction(ACTION_WIDGET_OFF);
        actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.offButton, actionPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_WIDGET_ON)) {
            Log.i("onReceive", ACTION_WIDGET_ON);
            new RestPostUtil().execute(serverUrl + "/lightsOn");
        } else if (intent.getAction().equals(ACTION_WIDGET_OFF)) {
            Log.i("onReceive", ACTION_WIDGET_OFF);
            new RestPostUtil().execute(serverUrl + "/lightsOff");
        } else {
            super.onReceive(context, intent);
        }
    }

}

【问题讨论】:

    标签: android button android-widget


    【解决方案1】:

    您将需要 TimerrunOnUiThread 来防止 anr 只需放置 TimeImMS 就可以了(例如 1000 = 1s)

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        button.setEnabled(false);
    
        Timer buttonTimer = new Timer();
        buttonTimer.schedule(new TimerTask() {
    
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        button.setEnabled(true);
                    }
                });
            }
        }, TimeInMS);
    }
    });
    

    【讨论】:

      【解决方案2】:

      尝试import static java.lang.Thread.sleep;

      然后使用

      try {
      sleep(3000);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        相关资源
        最近更新 更多