Android应用小工具 - 按钮来改变TextView的内容 [英] Android App Widget - Button to change TextView content

查看:177
本文介绍了Android应用小工具 - 按钮来改变TextView的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点糊涂了Android应用程序的部件。我知道我可以创建一个按钮来启动一个活动,但我不看我怎么可以创建一个按钮可以编辑窗口小部件内一个TextView的文本。 是否有人可以告诉我,我缺少的是什么?

I'm a little confused with Android app widgets. I understand that I can create a button to start an activity, but I don't see how I can create a button to edit the text of a TextView inside the widget. Can someone please tell me what I'm missing?

感谢很多提前。

编辑: 我的意思不是让用户输入新的文字,我的意思是应用程序自动更改文本,没有任何用户界面(例如,对话框或活动)。

I don't mean for the user to input the new text, I mean for the application to change the text automatically, without any UI (i.e., dialog boxes or activities).

推荐答案

编辑: 好了,现在你可以设定一个<一个href="http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast%28android.content.Context,%20int,%20android.content.Intent,%20int%29"相对=nofollow> getBroadcast PendingIntent您的按钮,<一个href="http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent%28int,%20android.app.PendingIntent%29"相对=nofollow> setOnClickPendingIntent 并执行您的更新(或致电update方法)与接收器的新文本。你 AppWidgetProvider 类可以是你的接收器(你可以明白你的意图<一个href="http://developer.android.com/reference/android/appwidget/AppWidgetProvider.html#onReceive%28android.content.Context,%20android.content.Intent%29"相对=nofollow>的onReceive ),否则你创建一个扩展的的BroadcastReceiver

OK, so you set a getBroadcast PendingIntent on your button with setOnClickPendingIntent and perform your update (or call your update method) with the new text from the receiver. Your AppWidgetProvider class can be your receiver (you can catch your intent in onReceive), otherwise you create a new class that extends BroadcastReceiver.

EDIT2(样品code):

EDIT2 (sample code):

我没有跑/编译这个code所以希望它不会有错误。 这是一个简单的辅助方法获得一个基本的更新未决意向的自定义操作:

I didn't run/compile this code so hopefully it will not have errors. This is a simple helper method to get a basic update pending intent with a custom action:

public PendingIntent getRefreshPendingIntent(Context context, int appWidgetId){
    Intent intent = new Intent("my.package.ACTION_UPDATE_WIDGET");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

赶上的onReceive自定义操作在你重写AppWidgetProvider类

Catch the custom action in onReceive in your overridden AppWidgetProvider class

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    //handle other appwidget actions    

    if (intent.getAction().equals("my.package.ACTION_UPDATE_WIDGET")) {
        //some code here that will update your widget
    }

    //handle other appwidget actions
}

您还需要进行自定义操作添加的IntentFilter到您的清单中appwidget接收机声明

You will also need to add an IntentFilter for the custom action to your manifest in the appwidget receiver declaration

<receiver
    android:name="my.package.MyWidgetProviderClass"
    android:label="MyWidget">
    <intent-filter>     
        <action android:name="my.package.ACTION_UPDATE_WIDGET"/>
        <!-- other intent filters -->
    </intent-filter>
    <!-- meta data pointing to widget xml file -->
</receiver>

最后,应用挂起意图,你RemoteViews在任何你正在建设它,单击按钮时,它会发送my.package.ACTION_UPDATE_WIDGET行动,将被开发者的AppWidgetProvider,在那里你可以(或叫法),执行你的AppWidget更新

Finally you apply the pending intent to your RemoteViews where ever you are building it so when the button is clicked it will send the my.package.ACTION_UPDATE_WIDGET action, which will be caught by your AppWidgetProvider, where you can (or call a method to) perform your AppWidget update

myRemoteViews.setOnClickPendingIntent(R.id.id_of_update_button, getRefreshPendingIntent(context, appWidgetId);

这篇关于Android应用小工具 - 按钮来改变TextView的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆