3

我需要做一个简单的小部件,点击它时会显示一个 Toast。我的问题是我找不到如何获取或创建“oncreate”操作。我看到了打开 Web 浏览器的未决意图的示例。但是我如何简单地创建它:Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();并在每次用户点击小部件时让它发生?为了清楚起见,我的意思是手机启动器上的一个小部件。不是常规的“按钮”小部件等...

public class Widget extends AppWidgetProvider {
NotificationManager mNotificationManager;
Notification notification;

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



    appWidgetManager.updateAppWidget(appWidgetIds, view);
}

}

谢谢!

4

6 回答 6

4

只需调用Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();小部件的onClick()方法View.OnClickListener

更新:

如果你使用AppWidgetProvider所以检查这个这个帖子

于 2012-09-20T11:49:00.603 回答
1

我认为您缺少展示创建的Toast. 例如:

Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();

此外,覆盖onClick自定义View类的方法并在那里弹出吐司。


public class TestButton extends Button {

    public TestButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "activated", Toast.LENGTH_LONG)
                  .show();
            }
        });
    }

}
于 2012-09-20T11:50:11.227 回答
0
Button btn=(Button) findViewById(R.id.button);

            btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
                   Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();


  }
}
});
于 2012-09-20T11:55:02.370 回答
0

试试这个:

button = (Button) findViewById(R.id.buttonToast);

        button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        // Create a piece of toast.
        Toast pieceToast = Toast.makeText(getApplicationContext(), "Test Message", Toast.LENGTH_SHORT);

        // Show the toast.
        pieceToast.show();
    }

这是我的 BroadcastReciever 类:

public class IncomingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if(null == bundle)
            return;
        Log.i("IncomingCallReceiver",bundle.toString());
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        String registredPhoneNumber;
        Log.i("IncomingCallReceiver","State: "+ state);
        String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        System.out.println("*****Mobile Ringing*******"+phonenumber);
        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {
            registredPhoneNumber = PreferenceConnector.getPhoneNumber(context);
            System.out.println("registredPhoneNumber:  "+registredPhoneNumber);
            System.out.println("phonenumber:   "+phonenumber);
            Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
            if(phonenumber.equals(registredPhoneNumber)){
                System.out.println("Entered...");
                String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
                if(isMyServiceRunning(context)){
                    context.stopService(new Intent(context,com.visiomaticamericas.visitormobile.services.LaunchServiceActivity.class));
                    System.out.println("******Service Stopped*********");
                }
                Intent i = new Intent(context,com.services.LaunchServiceActivity.class);
                i.putExtra("delay",500L);
                context.startService(i);
                System.out.println("*****Service Started*****");
                Toast.makeText(context, info, Toast.LENGTH_LONG).show();
            }

        }
    }


}

在这里,我调用了我的活动类..并在我的活动类中进行了小部件设计..

于 2012-09-20T11:55:58.490 回答
0

这可能会有所帮助,这里需要传递上下文并使用您的小部件获取 Onclick 事件

Toast.makeText(context, "激活", Toast.LENGTH_LONG).show();

于 2012-09-20T11:56:31.407 回答
0

伙计们,我的 VS 环境是 VS 2017 with Xamarin 。

   private void DisplayMessage(string DisplayMsgText)
    {
        Toast.MakeText(this.ApplicationContext, DisplayMsgText, ToastLength.Short).Show();
    }

并使用此功能在设备屏幕上显示消息,如下所示:

DisplayMessage("按下振动按钮");

于 2018-09-07T19:57:21.877 回答