关于选择特定下拉项的警报对话框

Alert Dialog on selection of particular drop down item

我做了一个 Spinner 城市 selection。我在那里显示了一些特定的城市(在我的项目中指定),dropDownList 中的最后一项是 "others"。我希望当用户 select "others" 时,对话框带有 TextInputLayout 要求用户输入城市,或者以任何其他方式。我只是希望在 selecting 其他人之后,我要求用户输入城市,并且该城市显示在微调框上。我是 Android Studio 的新手,似乎无法解决问题。

这是我到目前为止所做的。 我的 activity_main.xml

中的微调器布局
<Spinner
    android:id="@+id/citySpinner"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:spinnerMode="dialog" />

微调器字段中显示的 selected 文本样式布局

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fontFamily="@font/ubuntu_bold"
    android:textColor="@color/Blue"
    android:textSize="16sp">
</TextView>

下拉项的布局

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="@color/Blue"
    android:paddingBottom="12dp"
    android:paddingTop="12dp"
    android:paddingStart="12dp"
    android:fontFamily="@font/ubuntu_medium">
</TextView>

和我的主要 activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner citySpinner = findViewById(R.id.citySpinner);
        List<String> cityList = new ArrayList<String>();
        cityList.add("Select a City");
        cityList.add("Banglore");
        cityList.add("Pune");
        cityList.add("Mumbai");
        cityList.add("Noida/Ghaziabad");
        cityList.add("Other");

        ArrayAdapter<String> cityDataAdapter = new ArrayAdapter<String>(this, 
        R.layout.city_spinner_selected_item, cityList);  
        cityDataAdapter.setDropDownViewResource(R.layout.support_simple_
        spinner_dropdown_item);
        cityDataAdapter.setDropDownViewResource(R.layout.register_dropd
        own_item);
        citySpinner.setAdapter(cityDataAdapter);

}

现在,如何设置对其他人的警告对话框?

首先创建一个 OnItemSelectedListener 并使用 position == 5 你知道,当 others 被点击时。

citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(position == 5){
        //show your dialog
    }
}

});

然后你用 EditText.

创建一个对话框
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Enter City");

EditText editText = new EditText(this);
alert.setView(editText);


alert.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
       //Here you set the text of your EditText to your TextView
       yourtextview.setText(editText.getText().toString());
    }
});

alert.show();

然后你就把它放在你的 OnItemSelectedListener.

 citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(position == 5){


AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
    alert.setTitle("Enter City");

    EditText editText = new EditText(this);
    alert.setView(editText);


    alert.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
           //Here you set the text of your EditText to your TextView
           yourtextview.setText(editText.getText().toString());
        }
    });

    alert.show();
    }
    }

    });