1

下面是我的 MainActivity。

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private BottomSheetBehavior bottom_sheet_checkbox;
    private Button btnCity;
    private Toolbar toolbar;
    private Spinner sprCity;
    private ArrayList<SprModel> alCity;
    private SpinnerAdapter spinnerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getId();
        setSupportActionBar(toolbar);
        setListner();
        loadSpinner();
    }
    private void loadSpinner() {
        alCity = new ArrayList<SprModel>();
        alCity.add(new SprModel("Ahmedabad"));
        alCity.add(new SprModel("Vadodara"));
        alCity.add(new SprModel("Surat"));
        alCity.add(new SprModel("Mumbai"));

        spinnerAdapter = new SpinnerAdapter(MainActivity.this,alCity);
        sprCity.setAdapter(spinnerAdapter);
        spinnerAdapter.notifyDataSetChanged();
    }
    private void getId() {
        try {
            try {
                bottom_sheet_checkbox = BottomSheetBehavior.from(findViewById(R.id.bottomSheet_checkbox));
                toolbar = (Toolbar) findViewById(R.id.toolbar);
                btnCity = (Button)findViewById(R.id.btnCity);
                sprCity = (Spinner)findViewById(R.id.sprCity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void setListner() {
        try {
            btnCity.setOnClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) 
            {
            case R.id.btnCity:
                    bottom_sheet_checkbox.setState(BottomSheetBehavior.STATE_EXPANDED);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的 activity_main.xml

     <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/LayOut"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="android.com.bottomsheets.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            />

    </android.support.design.widget.AppBarLayout>

    <!--it is used to add one more layout on same layout.-->
    <include layout="@layout/content_main" />
    <include layout="@layout/bottom_sheet_checkbox" />

</android.support.design.widget.CoordinatorLayout>

这是 content_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center_horizontal"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnCity"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:text="Select city"
        android:textAllCaps="false"
        />
</LinearLayout>

这里定义了微调器。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheet_checkbox"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@android:color/darker_gray"
    android:padding="@dimen/activity_vertical_margin"
    app:behavior_hideable="true"
    app:behavior_peekHeight="60dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

   <Spinner
       android:id="@+id/sprCity"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:spinnerMode="dropdown"
       ></Spinner>
</RelativeLayout>

我已经实现Design support libraryBottom sheet我想在自定义布局上实现微调器的地方。我正在使用 arraylist 成功加载微调器和微调器加载,但是当我尝试选择微调器的任何项目时,它只显示第一个或默认项目,因此我无法从微调器中选择任何项目。

4

1 回答 1

0

我在我的情况下注意到这一点,其中选定微调器的值将发布在编辑文本中。经过一些研究后,我决定在编辑文本上更改焦点时调用 ongetselected

如图所示

   spinnerDocumentType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    textInputEditTextDocumentType.setText(documentType[i]);

                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                }
            });

设置我的编辑文本

 textInputEditTextDocumentType.setOnTouchListener(new View.OnTouchListener()
            {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    spinnerDocumentType.performClick();

                    return false;
                }
            });

更甜蜜的部分是

   textInputEditTextDocumentType.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if ( spinnerDocumentType.getCount() > 0 )
                    {
                        spinnerDocumentType.getOnItemSelectedListener()
                                .onItemSelected( spinnerDocumentType, null, spinnerDocumentType.getSelectedItemPosition(), 0 );
                        Log.e(TAG,"on touch spinner document");
                        String a=spinnerDocumentType.getSelectedItem().toString();
                        textInputEditTextDocumentType.setText(a);

                    }

                }
            });

仍在积极开发中,因为当焦点改变而不是选择微调器时,微调器会发生变化

于 2017-08-22T13:12:14.043 回答