質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
ウィジェット

ウィジェットとはユーザインタフェイスの要素(GUI widget)であるか、もしくは、独立した比較的サイズの小さいソフトウェアアプリケーション(desktop widget)のことを指します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1295閲覧

android studioで、クリックしたらテキストが変わるウィジェットについて。

noko123

総合スコア28

ウィジェット

ウィジェットとはユーザインタフェイスの要素(GUI widget)であるか、もしくは、独立した比較的サイズの小さいソフトウェアアプリケーション(desktop widget)のことを指します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2020/08/24 06:01

編集2020/08/24 06:08

android studioでテキストをクリックしたら"hallo"と表示が変更されるウィジェットを作ろうと思い、ネットにあったJavaで書かれたコードを参考に作ってみたのですが上手くいきませんでした。どこがおかしいのかを指摘していただけると有難いです。よろしくお願致します。

class AppWidget : AppWidgetProvider() { override fun onUpdate( context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray ) { // There may be multiple widgets active, so update all of them for (appWidgetId in appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId) } } override fun onEnabled(context: Context) { // Enter relevant functionality for when the first widget is created } override fun onDisabled(context: Context) { // Enter relevant functionality for when the last widget is disabled } override fun onReceive(context: Context?, intent: Intent?) { super.onReceive(context, intent) if(intent?.action.equals("CLICK_WIDGET")){ val changeWidgetId=intent?.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,0) val views2=RemoteViews(context?.packageName,R.layout.app_widget) views2.setTextViewText(R.id.textView5,"hello") val widgetManager2=AppWidgetManager.getInstance(context) if (changeWidgetId != null) { widgetManager2.updateAppWidget(changeWidgetId,views2) } } } } internal fun updateAppWidget( context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int ) { val views :RemoteViews=RemoteViews(context.packageName,R.layout.app_widget) val intent = Intent() intent.setAction("CLICK_WIDGET") intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId) val pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT) views.setOnClickPendingIntent(R.id.textView5,pendingIntent) appWidgetManager.updateAppWidget(appWidgetId,views)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.eigochamploo.widgetsample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".AppWidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="CLICK_WIDGET"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/app_widget_info" /> </receiver> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

val intent = Intent(context,AppWidget::class.java)と遷移先にウィジェットを指定したら出来ました。

投稿2020/08/24 08:19

noko123

総合スコア28

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問