Я написал простой код виджета. Кнопка «Добавить TextView» создает TextView N1, N2, N3, кнопка «Установить текст в TextView N2» должна изменить текст в TextView N2. Как изменить текст в TextView N2 или получить идентификатор TextView N2?
MainActivity.java
package ru.test.widget;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addTextView = findViewById(R.id.buttonAddTextView);
addTextView.setOnClickListener(this);
Button setText = findViewById(R.id.buttonSetText);
setText.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonAddTextView:
addTextView();
break;
case R.id.buttonSetText:
setText();
break;
}
}
private void addTextView() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
ComponentName thisWidget = new ComponentName(this, AppWidget.class);
int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget);
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.app_widget);
for (int i = 0; i < 3; i++) {
RemoteViews textView = new RemoteViews(getPackageName(), R.layout.text_view_layout);
textView.setTextViewText(R.id.textView1, "TextView number " + String.valueOf(i));
views.addView(R.id.appWidgetLinearLayout, textView);
}
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private void setText() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
ComponentName thisWidget = new ComponentName(this, AppWidget.class);
int[] appWidgetId = appWidgetManager.getAppWidgetIds(thisWidget);
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.app_widget);
views.setTextViewText(R.id.textView1, "Text !!!!");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
AppWidget.java
package ru.test.widget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
/**
* Implementation of App Widget functionality.
*/
public class AppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget);
views.setTextViewText(R.id.appWidgetText, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@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) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/buttonAddTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add TextView" />
<Button
android:id="@+id/buttonSetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set text in new TextView N2" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
app_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/app_widget"
android:initialLayout="@layout/app_widget"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen"></appwidget-provider>
app_widget.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">
<LinearLayout
android:id="@+id/appWidgetLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/appWidgetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:background="#000000"
android:text="@string/appwidget_text"
android:textColor="#ffffff"
android:textSize="8sp"/>
</LinearLayout>
</RelativeLayout>
text_view_layout.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:text="TextView"
android:background="#7E2E2E"
android:textColor="#ffffff"
android:textSize="8sp"/>