【问题标题】:How to change a TextView's text by pressing a Button in Android widget如何通过按下 Android 小部件中的按钮来更改 TextView 的文本
【发布时间】:2018-05-30 17:32:33
【问题描述】:

这是我的 res/xml/widget.xml

  <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-" />

这是我的 AppWidgetProvider 文件:

 @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            Intent intent = new Intent(context,MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
            views.setOnClickPendingIntent(R.id.button,pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId,views);

//            views.setTextViewText(R.id.textView,"2");
            updateAppWidget(context, appWidgetManager, appWidgetId);


        }
    }

然后按下按钮(+),textview 添加一个\textview=6。然后按下按钮(-),textview = 4 请帮助我,我需要任何解决方案。

【问题讨论】:

  • 我不明白如何在更改文本视图中使用挂起的意图?请帮帮我

标签: android widget android-pendingintent


【解决方案1】:
  1. 在您的 Activity/Fragment 中实现 View.OnClickListener。
  2. 创建一个静态整数计数。 private static int count;

  3. 在您的 Activity/Fragment 中添加以下逻辑:

        Button buttonPlus = findViewById(R.id.button);
        Button buttonMinus = findViewById(R.id.button3);
        textView = findViewById(R.id.textView);
    
        if (textView.getText().toString().isEmpty()) {
            count = 0;
        } else {
            count = Integer.valueOf(textView.getText().toString());
        }
    
        buttonPlus.setOnClickListener(this);
        buttonMinus.setOnClickListener(this);
    
  4. 在您实现的 onClick 中,添加以下内容:

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                count++;
                updateTextView(count);
                break;
            case R.id.button3:
                count--;
                updateTextView(count);
                break;
        }
    }
    
  5. 最后创建一个更新你的文本视图的方法:

    private void updateTextView(int count) {
        textView.setText(String.valueOf(count));
    }
    

【讨论】:

  • 谢谢,但我的活动是扩展 AppWidgetProvider。 Activity 没有 findViewById() 函数。
【解决方案2】:

您不能直接从 AppWidgetProvider 取消引用视图,您必须为此使用 Remote Views。 示例:-

private static final String MyOnClick = "Button OnClick";

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.you_widget_layout);
views.setOnClickPendingIntent(R.id.your_button, MyOnClick);

Get You Button Click Listener 像这样:-

@Override
public void onReceive(Context context, Intent intent) 
{
   if (intent.getAction().equals(MyOnClick)) //Update Here <- <-
   {
     views.setTextViewText(R.id.your_text_view, "text here");
   }
}

【讨论】:

  • 也许,我错了。错误:不兼容的类型:字符串无法转换为 PendingIntent
猜你喜欢
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多