Android-Show Custom Dialog

后端 未结 6 1713
忘了有多久
忘了有多久 2021-01-24 20:15

In my android application, I am using a custom dialog. When I try to show the dialog, it causes an error. I don\'t know what I am doing wrong, and I am really confused.

<
6条回答
  •  孤街浪徒
    2021-01-24 20:32

    i have created a CustomDialog. like this..

    enter image description here

    the xml is ..

        
    
    
        
    
            
    
            
    
            
    
            

    and CustomDialogClass is

        import android.app.Activity;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.view.View;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.Toast;
    
    import com.example.fragmentaltdd.R;
    
    public class CustomDialogClass extends Dialog implements
    android.view.View.OnClickListener {
    
        public Activity c;
        public Dialog d;
        public Button Post, Cancel,Upload;
    
        public CustomDialogClass(Activity a)
        {
            super(a);
    
            this.c = a;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
    
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            setContentView(R.layout.createpost);
    
            Post = (Button) findViewById(R.id.post_btn);
    
            Cancel = (Button) findViewById(R.id.cancel_btn);
    
            Upload = (Button)findViewById(R.id.upload_btn);
    
            Post.setOnClickListener(this);
    
            Cancel.setOnClickListener(this);
    
            Upload.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.post_btn:
                  //c.finish();
                    Toast.makeText(c, "Post button clikced", Toast.LENGTH_LONG).show();
                  break;
    
                case R.id.upload_btn:
                     // c.finish();
                    Toast.makeText(c, "Upload button clikced", Toast.LENGTH_LONG).show();
                      break;
    
                case R.id.cancel_btn:
                  dismiss();
                  break;
    
                default:
                  break;
            }
            dismiss();
        }
    }
    

    And call it like this

        CustomDialogClass cdd = new CustomDialogClass(getActivity());//while calling from fragment
    
    CustomDialogClass cdd = new CustomDialogClass(YourActivity.this);//while calling from Activity
    
                cdd.show();
    

提交回复
热议问题