0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

androidWidgetで二つ以上のボタンを作成する方法

Last updated at Posted at 2016-11-28

 ググって見たところ情報が少なかったのでまとめてみました。
今頃androidアプリなんてどんだけ初心者なんだよ。。となりますが許してください。
あと文章を書くのが下手なのでお見苦しいかもしれません。

widgetのサービスを作成

 今回は入力テキストと入力ボタン1,2と削除ボタンDELの4つのViewを設定しています。

new_app_widget_test.xml
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:padding="@dimen/widget_margin">

    <TextView
        android:id="@+id/appwidget_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="test"
        android:textSize="25dp"
        android:textStyle="bold|italic" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DEL"
        android:id="@+id/button"
        android:layout_below="@+id/appwidget_text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/button1"
        android:layout_below="@+id/appwidget_text"
        android:layout_toRightOf="@+id/button"
        android:layout_toEndOf="@+id/button" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/button2"
        android:layout_alignTop="@+id/button"
        android:layout_toRightOf="@+id/button1"
        android:layout_toEndOf="@+id/button1" />

</RelativeLayout>

 サービスクラスでViewを設定
 複数のボタンを作成すること想定しているのでfor文を使ってぶん回しています。

Myservice.java
public class MyService extends Service {

    private String text="";

    //サービスが起動されたら開始
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int[] viewId={R.id.button,R.id.button1,R.id.button2};
        String[] ACTION={"0","1","2"};


        //Viewの設定
        Context con=getBaseContext();
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.new_app_widget_test);
        remoteViews.setTextViewText(R.id.appwidget_text,text);//テキスト

        for (int i=0;i<viewId.length; i++){
            Intent buttonIntent1 = new Intent(con,MyService.class);
            buttonIntent1.setAction(ACTION[i]);
            PendingIntent pendingIntent1 = PendingIntent.getService(con, 0, buttonIntent1, 0);
            remoteViews.setOnClickPendingIntent(viewId[i],pendingIntent1);
        }

        // AppWidgetの画面更新
        ComponentName thisWidget = new ComponentName(MyService.this,NewAppWidget_test.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(MyService.this);
        manager.updateAppWidget(thisWidget, remoteViews);

        return super.onStartCommand(intent, flags, startId);
    }

   @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}


ボタンクリック後TextViewに反映

 入力ボタンからデータを反映させるために、onStartCommandメゾットに**SetText()**追加。この際、呼び出すタイミングをView設定より前に設定しておかないとViewに反映されません。

Myservice.java
  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //設定の変更を反映させる。
        SetText(intent.getAction());
        int[] viewId={R.id.button,R.id.button1,R.id.button2};
        String[] ACTION={"0","1","2"};

    ~~~以下省略~~~

 取得したデータをテキストに反映するためのメゾットを作成

Myservice.java
  private void SetText(String action) {

        if (action!=null) {
            //もしDELを選択されたら文字列を消す。
            if (action=="0"){
                text="";
            //それ以外は文字列を追加
            }else {
                text=text+action;
            }
        }
    }

以上でうまくいくいってるはずです。
Intentのパラメーターに設定する方法でも可能ですが、クラスファイルも少なく整理も行いやすいのでグローバル変数を用いて作成しています。そのため、プログラムコードが長くなる時は脆弱性が高まるかと思います。

今回開発したアプリは下のリンクに貼っておきます。
GitHub-Androidwidgetーテストアプリ

参考サイト
Android widgetで2つのボタンを検知する方法

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?