Yêu cầu Aug 20th, 2018 3:14 a.m. 147 0 1
  • 147 0 1
0

Cần giúp đỡ làm sự kiện on click button trên widget

Chia sẻ
  • 147 0 1

Cho em hỏi có ai làm widget cho android không giúp em làm cái sự kiện on click button trên widget với

1 CÂU TRẢ LỜI


Đã trả lời Aug 20th, 2018 3:44 a.m.
Đã được chấp nhận
+1

@Lynk Có lẽ bạn đang gặp vấn đề về việc xác định xem bạn nhấn vào button nào trên widget.

Khi tạo 1 PendingEvent, bạn chỉ cần setAction cho nó và trong hàm onReceive() bạn kiểm tra xem action đó là action nào rồi xử lý tiếp.

public class MyWidgetProvider extends AppWidgetProvider {
    private static final String MyOnClick1 = "myOnClickTag1";
    private static final String MyOnClick2 = "myOnClickTag2";
    private static final String MyOnClick3 = "myOnClickTag3";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate()");
        for (int widgetId : appWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName() ,R.layout.widget_layout);
            remoteViews.setOnClickPendingIntent(R.id.widget_button_stay, getPendingSelfIntent(context, MyOnClick1));
            remoteViews.setOnClickPendingIntent(R.id.widget_button_away, getPendingSelfIntent(context, MyOnClick2));
            remoteViews.setOnClickPendingIntent(R.id.widget_button_diss, getPendingSelfIntent(context, MyOnClick3));
            remoteViews.setTextViewText(R.id.widget_textview, "viblo abc");

            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }

    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(MyOnClick1)) {
            // your onClick action is here
            Toast.makeText(context, "Button1", Toast.LENGTH_SHORT).show();
            Log.w("Widget", "Clicked button1");
        } else if (action.equals(MyOnClick2)) {
            Toast.makeText(context, "Button2", Toast.LENGTH_SHORT).show();
            Log.w("Widget", "Clicked button2");
        } else if (action.equals(MyOnClick3)) {
            Toast.makeText(context, "Button3", Toast.LENGTH_SHORT).show();
            Log.w("Widget", "Clicked button3");
        }
    }
}
Chia sẻ
Avatar Kids @Lynk
Aug 20th, 2018 4:10 a.m.

Anh ơi ví dụ như em muốn đổi image của button thì như thế nào

Aug 20th, 2018 4:30 a.m.

@Lynk Bạn thử cách này xem.

Trong onReceive():

//.....
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

// Tham số thứ nhất là id của phần tử muốn đổi image
// Tham số thứ 2 là image/drawable muốn sử dụng
remoteViews.setImageViewResource(R.id.widget_button_away, R.drawable.border_radius_10);
Avatar Kids @Lynk
Aug 20th, 2018 8:10 a.m.

@DoanhPV anh ơi cái RemoteViews đó nếu gọi ở onReceice thì trên onUpdate có cần để không

Avatar Kids @Lynk
Aug 20th, 2018 8:27 a.m.

@DoanhPV anh Doanh ơi có rảnh giúp em với, em làm mãi mà không được ạ

Avatar Kids @Lynk
Aug 20th, 2018 9:01 a.m.

@DoanhPV

2018-08-20.png

code em như thế này sao nó không thay đổi hình ảnh nhỉ, anh giúp em với

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí