使用editText作为搜索框

时间:2014-10-14 07:08:38

标签: android search android-listview android-edittext

我有一个搜索图标,一个不可见的editeText和一个ListView。 ListView包含一些项目。 现在,当我单击搜索图像时,会出现editText,然后输入文本。假设出现ListView中的项目,单击该项目时,则会出现一个对话框。 我无法执行最后一部分。我认为可以使用矢量来完成。 请任何人都可以给我解决方案。 以下是供您参考的代码:

Spinner citySipinner;
EditText searchCity;
TextView header;
ImageView viewText;
ListView dealersList;
ArrayAdapter<String> adapter;
ArrayList<HashMap<String, String>> productList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    String city[] = {"Select city...", "A", "B"};

    String dealers[] = {"XYZ", "CDE"};


    viewText = (ImageView)findViewById(R.id.imageButton1);
    header = (TextView)findViewById(R.id.textHeader);
    dealersList = (ListView)findViewById(R.id.listView1);
    searchCity = (EditText)findViewById(R.id.editText1);
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, dealers);
    dealersList.setAdapter(adapter);

    viewText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (searchCity.getVisibility() == View.INVISIBLE) {
                 // showing the EditText
                searchCity.setVisibility(View.VISIBLE);
                searchCity.requestFocus();
                header.setVisibility(View.INVISIBLE);
                //Request Focus on EditText
             } else{
                 searchCity.setVisibility(View.INVISIBLE);
                 header.setVisibility(View.VISIBLE);
            }

        }
    });

    searchCity.addTextChangedListener(new TextWatcher() {

       @Override
       public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
           // When user changed the Text
           HomeActivity.this.adapter.getFilter().filter(cs);   
       }

       @Override
       public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
               int arg3) {


       }

       @Override
       public void afterTextChanged(Editable arg0) {

       }
   });



}

}

3 个答案:

答案 0 :(得分:0)

您应该使用getChildAt()作为ListView,当您将其作为一个视图时,可以将onClickListener添加到其中,如下所示:

dealersList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int pos,
                long arg3) {
            //the things you wanna to
            // you can just get the position and everything and make a switch-case statement
        }
    });

因此,你要做的事情,你想要弹出一个对话框。创建一个类并将其添加到项目的Manifest文件中,类似于下面的内容,提到它是一个对话框。

<activity
        android:name=".Dialog"
        android:label="@string/dialog"
        android:theme="@android:style/Theme.Dialog" >//this part is important to show that it's a dialog
        <intent-filter>
            <action android:name="com.example.app.DIALOG" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

现在在“你想要的东西”中你可以创建一个意图来进行这个新活动并将其视为如下对话框:

Intent dealerIntent = new Intent(Main.this, DIALOG.class);
                startActivity(dealerIntent);

答案 1 :(得分:0)

dealersList.setOnItemClickListener(this);

将此更改为....

dealersList.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
        {
            //the logic you need to implement
        }
    });

答案 2 :(得分:0)

可能会有所帮助

editTextPassword.addTextChangedListener(mTextEditorWatcher);

            }


            // EditTextWacther  Implementation

            private final TextWatcher  mTextEditorWatcher = new TextWatcher() {

                public void beforeTextChanged(CharSequence s, int start, int count, int after)
                {
                            // When No Password Entered
                           textViewPasswordStrengthIndiactor.setText("Not Entered");
                }

                public void onTextChanged(CharSequence s, int start, int before, int count)
                {

                }

                public void afterTextChanged(Editable s)
                {
                             if(s.length()==0)
                                    textViewPasswordStrengthIndiactor.setText("Not Entered");
                             else if(s.length()<6)
                                    textViewPasswordStrengthIndiactor.setText("EASY");
                             else if(s.length()<10)
                                    textViewPasswordStrengthIndiactor.setText("MEDIUM");
                             else if(s.length()<15)
                                    textViewPasswordStrengthIndiactor.setText("STRONG");
                               else
                                    textViewPasswordStrengthIndiactor.setText("STRONGEST");

                           if(s.length()==20)
                               textViewPasswordStrengthIndiactor.setText("Password Max Length Reached");
                }
        };