【问题标题】:How to implement a Dialog with a search box?如何实现带有搜索框的对话框?
【发布时间】:2013-02-28 19:51:11
【问题描述】:

我有一个带有列表的主要活动和一个接收关键字以在数据库中进行搜索的方法。

我想实现一个带有搜索框的AlertDialog,当我通过此表单中的动作侦听器单击按钮时,该搜索框应出现在主活动中

OnClickListener searchListener = new OnClickListener() {

    public void onClick(View v) {
        // ...
    }
};

这个对话框应该有一个简单的EditText 框和两个按钮(SearchCancel),并且必须通过EditText 选择的String到表单中的搜索方法:

public void searchWord(String keyword){
    // ....search in DB
    // ...updateListGUIWithNewValues()
}

此方法在主活动中获取新的列表值,以便活动可以更新 GUI 列表。

我已经实现了搜索方法和主要活动,但我不知道实现这种对话框的正确方法。

【问题讨论】:

标签: android search android-alertdialog


【解决方案1】:

这是我的警报对话框代码,其中包含编辑文本。我在按钮单击侦听器中调用它,因此它不是一个单独的方法。上周我在这个网站的某个地方找到了它,但我似乎找不到它,所以我可以引用它。

AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Manual Item Search");
alert.setMessage("Input Search Query");
// Set an EditText view to get user input 
final EditText input = new EditText(context);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
    String result = input.getText().toString();
        //do what you want with your result
        }
    });
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
         // Canceled.
        }
    });
alert.show();

【讨论】:

    【解决方案2】:

    您好,请尝试本教程使用警报对话框创建搜索对话框Click here

      AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
      alert.setTitle("Search Box");
      alert.setMessage("Message");
    
     // Set an EditText view to get user input 
     final EditText input = new EditText(this);
     alert.setView(input);
    
     alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText();
     // Do something with value!
       }
      });
      alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      // Canceled.
      }
     });
    
      alert.show();
    

    【讨论】:

      【解决方案3】:

      对话框 XML

      在您的 layout 文件夹中构建此 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:orientation="vertical" >
      
          <EditText
              android:id="@+id/text"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:inputType="text" />
      
          <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:weightSum="2" >
      
              <Button
                  android:id="@+id/search"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Search" />
      
              <Button
                  android:id="@+id/cancel"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Cancel" />
          </LinearLayout>
      
      </LinearLayout>
      

      对话框类

      这个类来处理对话框

      class dialog extends Dialog {
      
          private StackQuestionActivity activity;
          private Button search, cancel;
          private EditText text;
          private dialog thisDialog;
      
          public dialog(StackQuestionActivity context) {
              super(context);
              // TODO Auto-generated constructor stub
              this.activity = context;
              this.thisDialog = this;
          }
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              setContentView(R.layout.dialoge);
              getWindow().setLayout(android.view.ViewGroup.LayoutParams.FILL_PARENT,
                      android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
              initalize();
          }
      
          private void initalize() {
              // TODO Auto-generated method stub
              text = (EditText) findViewById(R.id.text);
              search = (Button) findViewById(R.id.search);
              cancel = (Button) findViewById(R.id.cancel);
              cancel.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      thisDialog.cancel();
                  }
              });
              search.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      String textString = text.getText().toString();
                      activity.searchWord(textString);
                  }
              });
          }
      
      }
      

      您的活动

      这是你的主要活动,按下按钮时调用对话框类:

      package com.example.question;
      
      import android.app.Activity;
      import android.app.Dialog;
      import android.content.Context;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      
      public class StackQuestionActivity extends Activity {
          /** Called when the activity is first created. */
          private Button press;
          private StackQuestionActivity activity;
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              this.activity = this;
              setContentView(R.layout.main);
              press = (Button) findViewById(R.id.button);
              press.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      dialog yourDialog = new dialog(activity);
                      yourDialog.show();
                  }
              });
          }
      
          public void searchWord(String textString) {
              // TODO Auto-generated method stub
      
          }
      }
      

      这是结果

      【讨论】:

      • 感谢您的回答但不起作用,应用程序崩溃并出现空指针异常,调用侦听器时出现错误
      • @AndreaF 不,没有错误,我在我的eclipse中编写代码,我给你的图像是结果,你的代码中的异常
      猜你喜欢
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2012-12-13
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多