Extending AlertDialogs in Android (where to find e

2019-01-23 22:04发布

I have been looking a lot for examples on how to correctly extend AlertDialogs and get the expected behaviour, but I can hardly find any.

The docs on google doesnt really say much either.

Yes, I know I can use a AlertDialog.Builder to build the most commons things, but for some reasons I want to create my own AlertDialogs (I have code that I want contained in separate java files for one reason).

I have created my PausDialog.java (see code below), it shows up but I am unable to get the title or any of the buttons (positive, negative etc) to show in the Dialog. See this picture:

No buttons and no title in the PausDialog

So, question 1: where can I find good, clean and useful examples on how to correctly extend AlertDialogs and how to use them thereafter

question 2: why can I not see the title or any buttons using the custom AlertDialog below?


PausDialog.java

package Test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PausDialog extends AlertDialog 
{

    protected PausDialog(Context context) 
    {
        super(context, R.style.DialogTheme);

    }

    @Override
    protected void onCreate(android.os.Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.paus);
        WindowManager.LayoutParams params = getWindow().getAttributes();  
        params.gravity = Gravity.TOP;

        final EditText ed1= (EditText) findViewById(R.id.editTextPausArea);
        final EditText ed2= (EditText) findViewById(R.id.EditTextPausTimeFrom);
        final EditText ed3= (EditText) findViewById(R.id.EditTextPausTimeTo);

        TextView tv1 = (TextView)findViewById(R.id.textViewPausArea);
        tv1.setText(LanguageHandler.GetString("AREA"));
        tv1 = (TextView)findViewById(R.id.textViewPausTime);
        tv1.setText(LanguageHandler.GetString("TIME"));

        setButton(DialogInterface.BUTTON_POSITIVE, "Positive",
            new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    int fromArea = 0;
                    int fromTime = 0;
                    int toTime = 0;

                    try
                    {
                        fromArea = Integer.parseInt(ed1.getText().toString());
                        fromTime = Integer.parseInt(ed2.getText().toString());
                        toTime = Integer.parseInt(ed3.getText().toString());

                    }
                    catch(Exception e)
                    {
                        // TODO fail
                    }                       
                }
            });

        setButton(DialogInterface.BUTTON_NEGATIVE, "Negative",
                new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something
                    }
                });
    }
}

MainActivity.java, calling the PausDialog:

PausDialog pd = new PausDialog(MainActivity.this);
pd.show();

The layout for my PausDialog, paus.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:weightSum="1">
    <RelativeLayout android:gravity="top" android:layout_height="200dp" android:layout_weight="0.11" android:layout_width="304dp">
        <TextView android:layout_alignParentLeft="true" android:text="Område" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textViewPausArea" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="17dp" android:layout_marginTop="18dp"></TextView>
        <EditText android:layout_alignBaseline="@+id/textViewPausArea" android:id="@+id/editTextPausArea" android:layout_width="80dp" android:layout_alignBottom="@+id/textViewPausArea" android:inputType="number" android:layout_height="wrap_content" android:layout_toRightOf="@+id/textViewPausArea" android:layout_marginLeft="17dp">
            <requestFocus></requestFocus>
        </EditText>
        <TextView android:text="Tid" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_below="@+id/editTextPausArea" android:id="@+id/textViewPausTime" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_alignLeft="@+id/textViewPausArea"></TextView>
        <EditText android:layout_alignBaseline="@+id/textViewPausTime" android:id="@+id/EditTextPausTimeFrom" android:layout_width="80dp" android:layout_alignBottom="@+id/textViewPausTime" android:inputType="time" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editTextPausArea"></EditText>
        <TextView android:text=" - " android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView3" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/EditTextPausTimeFrom" android:layout_alignBottom="@+id/EditTextPausTimeFrom" android:layout_toRightOf="@+id/EditTextPausTimeFrom"></TextView>
        <EditText android:id="@+id/EditTextPausTimeTo" android:layout_width="80dp" android:inputType="time" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_toRightOf="@+id/textView3"></EditText>    

    </RelativeLayout>

</LinearLayout>

4条回答
ゆ 、 Hurt°
2楼-- · 2019-01-23 22:16

Below is simple example from Android Documentation,

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message) 
       .setTitle(R.string.dialog_title); // Your custom title and message 

// 3. Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
           }
       });

builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User cancelled the dialog
           }
       });

// 4. Get the AlertDialog from create()
AlertDialog dialog = builder.create();

Both methods for setMessage() and setTitle() available from builder class itself.

In case you want to create custom view then use,

setView(int layoutResId) from builder class

查看更多
唯我独甜
3楼-- · 2019-01-23 22:19

There are few options to create custom AlertDialog. I just would like to give you answer for current question. You can set title, message and other components of AlertDialog in onCreate() method. But make sure you doing it before you calling super.onCreate() Example:

public class PausDialog extends AlertDialog {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                View content = LayoutInflater.from(getContext()).inflate(R.layout.dialog_some_view, null);
                setView(content);
                setTitle("Some Title");
                setMessage("Some Message");
                setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       Log.d("TAG", "BUTTON_POSITIVE");
                   }
                });
                ((TextView) content.findViewById(R.id.data)).setText(R.string.some_custom_data);
                ((TextView) content.findViewById(R.id.description)).setText(getContext().getString(R.string.description));
                setCancelable(false);
                setOnKeyListener((dialog, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK);

                super.onCreate(savedInstanceState);
            }
}
查看更多
来,给爷笑一个
4楼-- · 2019-01-23 22:27

I got my custom AlertDialog to work using something similiar to this code.

First make your constructor publicly accessible

public PausDialog(Context context)

Then you can simply instantiate and show it as so:

PauseDialog newDialog = new PauseDialog(this);
newDialog.setTitle("My Dialog");
newDialog.setButton("OK", ...); // Insert your onClick implementation
newDialog.setButton2("Cancel", ...); // Insert your onClick implementation
newDialog.Show();
查看更多
beautiful°
5楼-- · 2019-01-23 22:43

I've successfully made my own custom AlertDialog by extending the Builder class.

public class MyDialog extends AlertDialog.Builder {
    private Context mContext;
    private AlertDialog mAlertDialog;

    public MyDialog(Context context) {
        super(context);
        mContext = context;
    }

    @SuppressLint("InflateParams")
    @Override
    public AlertDialog show() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.my_dialog, null);

        ...
        mAlertDialog = super.show();
        return mAlertDialog;
    }

}

Then, from the code, I instantiate them like this:

MyDialog myDialog = new MyDialog(getActivity());
myDialog.show();

The only caveat is you have to take care of dismissing the dialog properly on orientation change or other events that don't explicit call the dismiss() method, like the back button or something else. Otherwise, you'll have memory leaks from that dialog.

查看更多
登录 后发表回答