关闭自定义对话框的android [英] close custom Dialog android

查看:127
本文介绍了关闭自定义对话框的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了微调和OK键自定义对话框。我已填充此微调了一些项目,虚增layout.If我点击确定按钮,对话框将消失。
我设置了微调

I created a custom dialog with spinner and OK button. I have populated this spinner with some items and inflated the layout.If I click OK button dialog will dismiss. I set the spinner

spinner.performCLick(); 

有没有什么办法让微调选定的项目,并关闭而不pressing OK按钮关闭对话框。我曾尝试

is there is any way to get spinner selected item and to close the dialog without pressing OK button. I have tried

button.performclick(); 

但没有用。

推荐答案

见下面我的 code 它可以帮助你。

see my below code it may help you.

package com.Test_dia;

import android.app.Activity;

import android.app.Dialog;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.Spinner;

import android.widget.Toast;

public class Test_diaActivity extends Activity {

    private Button btn;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showalert();
            }
        });
    }

    protected void showalert() {
        // TODO Auto-generated method stub
        final Dialog dia = new Dialog(this);
        dia.setContentView(R.layout.dia);
        final String a[] = { "select one", "android", "java", "php" };

        Button btnok = (Button) dia.findViewById(R.id.button2);
        Spinner spin = (Spinner) dia.findViewById(R.id.spinner1);

        btnok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dia.dismiss();
            }
        });

        spin.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, a));

        spin.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                if (arg2 > 0) {
                    Toast.makeText(Test_diaActivity.this,
                            "You Selected :" + a[arg2], Toast.LENGTH_SHORT)
                            .show();
                    dia.dismiss();
                }
            }

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

            }
        });

        dia.show();
    }
}

的main.xml

<LinearLayout 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click here" />

</LinearLayout>

dia.xml

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/spinner1"
    android:text="ok" />

这code是为我工作完全确定。

This code is work for me perfectly ok.

享受....

这篇关于关闭自定义对话框的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆