簡體   English   中英

Android主屏幕小部件-搜索

[英]Android homescreen Widget - Search

我想知道是否可以創建一個帶有搜索欄的Android HomeScreen小部件,當用戶輸入一些文本並按Enter時,它將打開相應的應用並在其中進行搜索。

我目前有一個包含徽標和文本視圖的小部件,但是,當您單擊該小部件時,沒有任何反應。 鍵盤甚至沒有彈出。 有人可以幫忙嗎?

目前,我將它隨機設置為具有隨機數的textview,然后單擊以更改數字

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:src="@drawable/logo_menu" />

    <TextView
        android:id="@+id/widget_search_textview"
        android:layout_width="0dp"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="10dp"
        android:clickable="true"
        android:layout_weight="1"
        style="@style/FontStyle.edit_text_view_light" />

</LinearLayout>

表現:

<receiver android:name="android.widget.SearchWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/search_widget_provider" />
        </receiver>

類:

public class SearchWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

      // Get all ids
      ComponentName thisWidget = new ComponentName(context,
              SearchWidget.class);
      int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
      for (int widgetId : allWidgetIds) {
        // create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.search_widget_layout);
        // Set the text
        remoteViews.setTextViewText(R.id.widget_search_textview, String.valueOf(number));

        // Register an onClickListener
        Intent intent = new Intent(context, SearchWidget.class);

        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.widget_search_textview, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
      }
    }

}//End of Search Widget Class

應用提供商:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="50dp"
    android:minHeight="50dp"
    android:initialLayout="@layout/search_widget_layout"
    android:resizeMode="horizontal"
    />

TextView不接受用戶輸入。 TextView是用於輸出的標簽。

在活動中,您可以使用EditText進行用戶輸入。 應用小部件不支持EditText

因此,對您的問題:

我想知道是否可以創建一個帶有搜索欄的Android HomeScreen小部件,當用戶輸入一些文本並按Enter時,它將打開相應的應用並在其中進行搜索。

這不是嚴格可行的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM