Android: Dialog dismisses without calling dismiss

前端 未结 2 1618
星月不相逢
星月不相逢 2020-12-22 04:37

I have a dialog which performs some validation (below). Thee problem is, the dialog is dismissed after the Toast is displayed, without me calling dismiss. I need to show the

相关标签:
2条回答
  • 2020-12-22 04:41

    My guess is that you are not creating and showing dialog as mentioned in the Android docs here http://developer.android.com/guide/topics/ui/dialogs.html using OnCreateDialog functions

    Please do as mentioned in the docs and let us know if it still does not work.

    0 讨论(0)
  • 2020-12-22 04:59

    I think whatever you are trying to achieve is not possible with AlertDialog.bilder instead of that you can make

    1. object of Dialog.
    2. Set your layout for your dialog.
    3. Set the appropriate listener.

    Example.

    dialog_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" android:orientation="vertical">
    
    <EditText 
                android:layout_height="wrap_content" 
                android:id="@+id/EditText01" android:layout_width="300dip" android:ellipsize="none"/>
    
    <LinearLayout 
            android:id="@+id/LinearLayout01" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
    
            <Button 
                    android:id="@+id/Button01" 
                    android:layout_height="wrap_content" 
                    android:text="Yes" 
                    android:layout_width="100dip"/>
    
            <Button 
                    android:id="@+id/Button02" 
                    android:layout_height="wrap_content" 
                    android:text="No" 
                    android:layout_width="100dip"/>
    </LinearLayout>
    
      </LinearLayout>
    

    Help.java

    public class Help extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        d =  new Dialog(Help.this,
                android.R.style.Theme_InputMethod);
    
        createMyDialog();
    }
      private Dialog d;
    private void createMyDialog() {
        d.setContentView(R.layout.dialog_view);
        Button b1 = (Button)findViewById(R.id.Button01);
        Button b2 = (Button)findViewById(R.id.Button02);
        EditText t = (EditText) findViewById(R.id.EditText01);
        OnTouchListener listner1 = null;
        OnTouchListener listner2 = null;
        b1.setOnTouchListener(listner1);
        b2.setOnTouchListener(listner2);
        listner1 = new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }
        };
        listner2 = new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }
        };
                d.show();
    }
    

    }

    0 讨论(0)
提交回复
热议问题