2016-09-23 11 views
0

私は4x1のサイズ変更可能なウィジェットを持っています。私がアイコンをクリックすると、それはうまく動作します
私のアプリを開きますが、バックグラウンドをクリックすると何もしません。
私の背景をクリック可能にする方法はありますか? description ここに私のコードです:アンドロイド画面でウィジェットの背景をクリックする方法

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example"> 
    <application> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
      </intent-filter> 
      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/myWidgetXml"/> 
     </receiver> 
    </application> 
</manifest> 

myWidgetXml.xml

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialKeyguardLayout="@layout/myLayout" 
    android:initialLayout="@layout/myLayout" 
    android:minHeight="40dp" 
    android:minWidth="40dp" 
    android:resizeMode="horizontal" 
    android:updatePeriodMillis="86400000" 
    android:widgetCategory="home_screen"> 
</appwidget-provider> 

myLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/widget_margin" 
    android:layout_margin="0dp" 
    android:gravity="center" 
    android:background="@android:drawable/alert_dark_frame" 
    android:orientation="vertical"> 

    <ImageButton 
     android:id="@+id/WidgetButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:src="@drawable/widget_pic" /> 
    <TextView 
     android:id="@+id/WidgetTextView" 
     android:textColor="@android:color/white" 
     android:textSize="14sp" 
     android:layout_marginTop="6dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" /> 
</LinearLayout> 

MyWidget.java

public class MyWidget extends AppWidgetProvider { 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     for (int appWidgetId : appWidgetIds) 
      updateAppWidget(context, appWidgetManager, appWidgetId); 
    } 

    private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) { 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout); 
     Intent intent = new Intent(context, MainActivity.class); 
     Uri.Builder builder = new Uri.Builder(); 
     intent.setData(builder.build()); 
     intent.setAction(Intent.ACTION_VIEW); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent); 
     views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic); 
     views.setTextViewText(R.id.WidgetTextView, "text"); 
     _appWidgetManager.updateAppWidget(WidgetId, views); 
    } 
} 
+0

はこの 'アンドロイドを試してみてください: 'linear layout'セクションの' myLayout.xml'の中でclickable = "true" '、' 'background'を' src'にchnageしようとしました – Mikhail

+1

@Mikhail、それはうまくいきませんでしたが、 - ) –

答えて

1

だけrootviewに、ではないのImageButtonにリスナーをクリックしてください設定。

今あなたが持っている:

views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent); 

は、例えば、あなたのルートのLinearLayoutにIDを与えます。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/rootView" 

し、ウィジェットなどのビューにsetOnClick()メソッドを適用してような単純な動作しませんウィジェットクリックでR.id.rootView

+1

多くのありがとう、素晴らしい答え:) –

+0

私はそれをバックグラウンドに変更したときクリックは機能しませんでしたが、アイコンをクリックしていませんでしたので、奇妙に見える場合でも両方のリスナーを設定しました –

+1

LinearLayout属性androidに追加してください:descendantFocusability = "blocksDescendants" およびImageButton clickable = false、focusable = false –

0

にここ

views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent); 

views.setOnClickPendingIntent(R.id.rootView, pendingIntent); 

R.id.WidgetButtonに変更リモートビューを使用します。クリックイベントを処理するための

は、次の操作を行います。id = "@ + ID/lnr_widget_back" あなたAndroidManifestで

  • :あなたのmyLayout.xmlで

    1. は のLinearLayoutのアンドロイドである、あなたの親のレイアウトにIDを追加します。 xmlファイル:

    たとえば、ルートLinearLayoutにIDを指定します。

    <receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" > 
    <intent-filter> 
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE"> 
    </action> 
    </intent-filter> 
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/myWidgetXml"/> 
    </receiver> 
    

    以下のようなあなたの意図-フィルターに1つのより多くのアクションを追加します。

    <action android:name="CLICK.MY_APP.WIDGET" ></action> 
    

    を注:あなたはこのアクションに任意の名前を与えるが、それはそれ自身の方法でユニーク であることを確認することができます。ユニークでない場合、 クリックイベント/ を受け取ることができない場合があります。3. MyWidgetで。javaファイル:

    - >書き換えてonReceive()メソッド:

    @Override public void onReceive(Context context, Intent intent) { 
    if(intent.getAction().equals("CLICK.MY_APP.WIDGET")) { // Your view 
    is clicked // Code for after click } super.onReceive(this.context, 
    intent); } 
    

    - あなたのupdateAppWidget()メソッドのセットで>あなたのクリックを処理する意図保留:

    Intent intent = new Intent("CLICK.MY_APP.WIDGET"); PendingIntent 
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT); 
    views.setOnClickPendingIntent(R.id.lnr_widget_back, pendingIntent); 
    
  • 関連する問題