选择项目后,如何关闭/取消/关闭微调器?

时间:2012-12-15 17:49:28

标签: android

当我选择一个选项它会打开一个活动但是当我完成该活动并且我返回了微调器活动时,微调器仍然是打开的。

如何在选择项目后立即解雇?

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();

    }

2 个答案:

答案 0 :(得分:2)

它需要关闭的对话框而不是微调器。因此,将AlertDialog声明为字段

并更改对话框如下所示。

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

在onItemSelected中添加

dialog.dismiss();

答案 1 :(得分:1)

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

你不必担心,它会自行关闭。