需要在Activity启动时在对话框中显示AutoComplete TextView

时间:2017-08-24 12:35:53

标签: android dialog android-alertdialog autocompletetextview

autotextview中有一个toolbar我需要在dialogalert dialogbox上查看相同内容。我已为此searchview人设置了建议,请以最简单的方式帮助我在autocomplete上添加dialog。{/ p>

我的代码如下:

AutoCompleteTextView autoservice; 
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grids);
        sharedPreferences = getSharedPreferences(MyPref, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        keypad= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
         autoservice=(AutoCompleteTextView)findViewById(R.id.auto);
        if(sharedPreferences.getString("locationStatus","")!="")
            autoservice.setText(sharedPreferences.getString("locationStatus","").substring(0,1).toUpperCase()+sharedPreferences.getString("locationStatus","").substring(1));
        final String[] web = getResources().getStringArray(R.array.web);
        TypedArray ar = getResources().obtainTypedArray(R.array.imageId);
        int length = ar.length();
        final int[] imageId = new int[length];
        for (int i = 0; i < length; i++)
            imageId[i] = ar.getResourceId(i, 0);
        ar.recycle();
        autoservice.setThreshold(2);
        autoservice.addTextChangedListener(this);


        autoservice.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                autoservice.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;

                autoservice.setText(null);
                    }
        });


        autoservice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                keypad.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                autoservice.setFocusable(false);
                autoservice.setFocusableInTouchMode(true);
                autoservice.getLayoutParams().width = LinearLayout.LayoutParams.WRAP_CONTENT;
                autoservice.setText((String)parent.getItemAtPosition(position));
                editor.putString("locationStatus",(String)parent.getItemAtPosition(position));
                editor.commit();
                locationStatus = sharedPreferences.getString("locationStatus", "");
                 }
        });

这是我的 toobar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- Irrelevant stuff -->
    <!-- Must be last for right layering display -->
    <FrameLayout
        android:id="@+id/toolbar_container"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"

            android:background="@drawable/buttonshape"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">
        <AutoCompleteTextView
            android:background="@drawable/button_shape"
            android:id="@+id/auto"
            android:textStyle="bold"
            android:textSize="15sp"
            android:hint="Location"
            android:textColorHint="#BDBDBD"
            android:drawableRight="@mipmap/ic_keyboard_arrow_down_white_24dp"
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true" />
        </android.support.v7.widget.Toolbar>

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            style="@style/MaterialSearchViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>
</FrameLayout>

这里给出的是我的xml文件和java文件,提前谢谢,对不起我的英文

0 个答案:

没有答案