AutoCompleteTextView实现对AlertDialog [英] AutoCompleteTextView implementation on the AlertDialog

查看:126
本文介绍了AutoCompleteTextView实现对AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OnClickListener方法来实现AutoCompleteTextView。但我没有看到下拉文本,一旦我点击AutoCompleteTextView仍然键盘出现。我喜欢有是下拉文本显示和键盘应该是不可见的。可能是什么问题? 我的源$ C ​​$ c是

I implemented AutoCompleteTextView in the OnClickListener method. But I don't see the drop down text once I click the AutoCompleteTextView and still keypad is appearing. What I like to have is drop down text appears and keypad should be invisible. What could be wrong? My source code is

public void runNextTask(){
        final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);                     
        final TrackerInfo newInfo = new TrackerInfo();


        //autocompletetextview 1
        final String[] type = {"Vehicle", "Person", "Pet", "Others"};   
        final AutoCompleteTextView actvtype1 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView1);
        actvtype1.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
                ArrayAdapter<String> typeadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,type);
                actvtype1.setThreshold(1);
                actvtype1.setAdapter(typeadapter);
                actvtype1.setTextColor(Color.BLACK);
            }

        });


        //autocompletetextview 2
        final String[] model = {"TS102", "TS103"};
        final AutoCompleteTextView actvtype2 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView2);
        actvtype2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Service.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(actvtype2.getWindowToken(), 0);
                ArrayAdapter<String> modeladapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,model);
                actvtype2.setThreshold(1);
                actvtype2.setAdapter(modeladapter);
                actvtype2.setTextColor(Color.BLACK);
            }
        });



        new AlertDialog.Builder(this).setTitle("New Tracker").setView(addView)
        .setPositiveButton("ADD", new DialogInterface.OnClickListener() 
        {
            @SuppressLint("SimpleDateFormat")
            public void onClick(DialogInterface dialog, int whichButton) 
            {                   
                TextView temp;


                temp = (TextView)addView.findViewById(R.id.IDeditText);
                newInfo.setIdnumber(temp.getText().toString());

                newInfo.setDescription(actvtype1.getText().toString());

                temp = (TextView)addView.findViewById(R.id.SimCardEdit);
                newInfo.setSimcardnumber(temp.getText().toString());


                newInfo.setModel(actvtype2.getText().toString());

                simnumber = temp.getText().toString();
                if(!simnumber.matches("[+0-9]+")){

                    return;
                }
                SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
                Date today = Calendar.getInstance().getTime(); 
                String reportDate = df.format(today);
                newInfo.setTime(reportDate);
                newInfo.setActivationStatus();
                if(!newInfo.getIdnumber().equals("") && !newInfo.getModel().equals("") 
                        && !newInfo.getDescription().equals("") && !newInfo.getSimcardnumber().equals(""))
                {
                      addNewTracker(newInfo);
                }

            }
        }).setNegativeButton("Cancel", null).show();

    }

我的XML是如下

My xml is as follow

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/IDnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/IDnumber" />

<EditText
    android:id="@+id/IDeditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:inputType="text">        
    <requestFocus />
</EditText>

<TextView
    android:id="@+id/SimCardNum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Sim_card_number" />

<EditText
    android:id="@+id/SimCardEdit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="text"
     />

<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/description" />   

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10" 
    android:text=""

 />   

<TextView
    android:id="@+id/model"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Tracker_model"         
    />    
<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10" 
    android:text=""
 />

推荐答案

现在,我想所有的工作。 我有两个的EditText 和两个 AutoCompleteTextView 。 我想说明的键盘只为的EditText 键,但 AutoCompleteTextView 键盘被隐藏。只显示下拉列表。现在,所有的工作,因为我喜欢的人。我分享我的工作。

Now all work as I want to be. I have two EditText and two AutoCompleteTextView. I want to show Keyboard only for the EditText and but for AutoCompleteTextView Keyboard is hidden. Only show dropdown list. Now all work as I like to be. I share my work.

final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null); 

        final TrackerInfo newInfo = new TrackerInfo();          

        String[] type = {"Vehicle", "Person", "Pet", "Others"}; 
        final AutoCompleteTextView actvtype1 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView1);     
        actvtype1.setInputType(InputType.TYPE_NULL);
        final ArrayAdapter<String> typeadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,type);    
        actvtype1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                actvtype1.showDropDown();
                actvtype1.setAdapter(typeadapter);
                actvtype1.setTextColor(Color.BLACK);

            }
        });


        String[] model = {"TS102", "TS103"};
        final AutoCompleteTextView actvtype2 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView2);
        actvtype2.setInputType(InputType.TYPE_NULL);
        final ArrayAdapter<String> modeladapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,model);
        actvtype2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                actvtype2.showDropDown();
                actvtype2.setAdapter(modeladapter);
                actvtype2.setTextColor(Color.BLACK); 

            }
        });


        final AlertDialog.Builder alert = new AlertDialog.Builder(this).setTitle("New Tracker").setView(addView);

        alert.setPositiveButton("ADD", new DialogInterface.OnClickListener() 
        {


            @SuppressLint("SimpleDateFormat")
            public void onClick(DialogInterface dialog, int whichButton) 
            {                   
                TextView temp;
                temp = (TextView)addView.findViewById(R.id.IDeditText);
                newInfo.setIdnumber(temp.getText().toString());                                 
                newInfo.setDescription(actvtype1.getText().toString());                 
                temp = (TextView)addView.findViewById(R.id.SimCardEdit);
                newInfo.setSimcardnumber(temp.getText().toString());                    
                newInfo.setModel(actvtype2.getText().toString());   
                if(!(actvtype2.getText().toString().equals("TS102")||actvtype2.getText().toString().equals("TS103")))
                {
                     Toast.makeText(getBaseContext(), "Model type doesn't match",
                                Toast.LENGTH_SHORT).show();
                     return;
                }
                simnumber = temp.getText().toString();
                if(!simnumber.matches("[+0-9]+")){

                    return;
                }
                SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
                Date today = Calendar.getInstance().getTime(); 
                String reportDate = df.format(today);
                newInfo.setTime(reportDate);
                newInfo.setActivationStatus();
                if(!newInfo.getIdnumber().equals("") && !newInfo.getModel().equals("") 
                        && !newInfo.getDescription().equals("") && !newInfo.getSimcardnumber().equals(""))
                {
                      addNewTracker(newInfo);
                }

            }
        }).setNegativeButton("Cancel", null).show();

XML是

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/IDnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/IDnumber" />

<EditText
    android:id="@+id/IDeditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:inputType="text">        
    <requestFocus />
</EditText>

<TextView
    android:id="@+id/SimCardNum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Sim_card_number" />

<EditText
    android:id="@+id/SimCardEdit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="text"
     />

<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/description" />   

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:dropDownHeight="200dp"  
    android:text=""/>

<TextView
    android:id="@+id/model"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Tracker_model"         
    />    
<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"         
    android:dropDownHeight="100dp"  
    android:text=""/>

<View
    android:layout_width="1dp"
    android:layout_height="50dp">
</View>

这篇关于AutoCompleteTextView实现对AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆