0

当我选择一个选项时,它会打开一个活动,但是当我完成该活动并返回微调器活动时,微调器仍处于打开状态。

选择项目后如何立即关闭它?

sp.setOnItemSelectedListener(new OnItemSelectedListener() {
            boolean firstPop =true;
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                if (!firstPop) {

                    doMyLogic();
                    sp.????
                }
                firstPop = false;

            }

编辑完整代码

public void showDropDownDialogue() {

        String[] s = getResources().getStringArray(R.array.cities);
        final ArrayAdapter<String> adp = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_spinner_item, s);

        final Spinner sp = new Spinner(MainActivity.this);
        sp.setPadding(5, 5, 5, 5);
        sp.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        sp.setAdapter(adp);


        sp.setOnItemSelectedListener(new OnItemSelectedListener() {
            boolean firstPop =true;

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                if (!firstPop) {
                    editor.putInt("city_id", pos);
                    editor.commit();
                    Intent stationsIntent = new Intent(MainActivity.this,
                    StationsActivity.class);
                    startActivity(stationsIntent);


                }
                firstPop = false;

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setView(sp);
        builder.create().show();

    }
4

2 回答 2

2

它是需要关闭的对话框而不是微调器。所以将 AlertDialog 声明为一个字段

并更改对话框显示如下。

builder.setView(sp);
dialog = builder.create();
dialog.show();

并在 onItemSelected 添加

dialog.dismiss();
于 2012-12-15T18:10:56.143 回答
1

您想实现 OnItemSelectedListener 并覆盖 onItemSelected 方法获取微调器的选定事件,它将关闭,您不需要以编程方式关闭

你不用担心,它会自动关闭。

于 2012-12-15T17:53:37.037 回答