我怎样才能将我的警报对话变成 android 中的自定义警报

How can i make my alert dialogue into a custom alert in android

如何将我的提醒对话框变成自定义提醒 android.please 帮助我在自定义提醒中显示我的对话框。我的代码粘贴在这里。 该代码包含 10 个列表视图的输出。当点击列表中的项目时,它会被提醒..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Find the ListView resource. 
    mainListView = (ListView) findViewById( R.id.mainListView );
   // Create and populate a List of planet names.
   final String[] planets = new String[] { "Allu", "Abin", "Bibin", "Aswathy",
                                      "Jibin", "Saran", "Jobin", "Neethu","ammu","Ram"};  
    final ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll( Arrays.asList(planets) );

    // Create ArrayAdapter using the planet list.
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

    /*// Add more planets. If you passed a String[] instead of a List<String> 
    // into the ArrayAdapter constructor, you must not add more items. 
    // Otherwise an exception will occur.
    listAdapter.add( "Ceres" );
    listAdapter.add( "Pluto" );
    listAdapter.add( "Haumea" );
    listAdapter.add( "Makemake" );
    listAdapter.add( "Eris" );*/

    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter ); 
    mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here
            alert.setMessage("Edit Your Name Here"); //Message here

            final EditText input = new EditText(context);
            input.setText((String)planetList.get(position));
            alert.setView(input);

            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String srt = input.getEditableText().toString();

                    Toast.makeText(context,srt, Toast.LENGTH_LONG).show();
                    planetList.set(position, srt);
                    listAdapter.notifyDataSetChanged();

                }
            });
            alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            }); //End of alert.setNegativeButton
            AlertDialog alertDialog = alert.create();
            alertDialog.show();
        }
    });

  }
}

为什么不能使用对话框?

        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog);//create an xml and use it.
        dialog.setCancelable(true);
        passwordEmail = (EditText) dialog
                .findViewById(R.id.dialog_txt_name);
                        dialog.show();

public class MyDialog extends DialogFragment {

public static MyDialog getInstance() {
    MyDialog dialog = new MyDialog ();
    Bundle bundle = new Bundle();

    bundle.putInt("your key", "save something");

    dialog.setArguments(bundle);

    return dialog;
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder vBuilder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View view = inflater.inflate(R.layout."your custom layout", null);

    yourEditText = (TextView) view.findViewById(R.id."your edit text id");

    Bundle bundle = getArguments();

    if(bundle!=null){

        ... do something
    }


    vBuilder.setView(view);

    return vBuilder.create();
}

}

打开此对话框

MyDialog.getInstance().show(getFragmentManager(), "your TAG");

而不是这个:

final EditText input = new EditText(context); input.setText((String)planetList.get(position)); alert.setView(input);

这样写:
LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(R.layout."your custom layout", null); alert.setView(view);

这是使用自定义 xml 布局制作弹出式对话框的简单方法,一定会奏效:

为您的对话框创建一个 xml 文件。设置 android:layout_width="wrap_content"android:layout_height="wrap_content" 或任何其他大小。您还可以为布局设置背景。

例如,这是一个 xml 弹出窗口布局 window:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/black" >

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:paddingBottom="5dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="title"
        android:textColor="@color/white"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <TextView
            android:id="@+id/details_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:paddingTop="40dp"
            android:paddingBottom="20dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Enter your name here:"
            android:textColor="@color/white"
            android:layout_weight="1"
            android:layout_gravity="center"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_gravity="center"
            android:layout_marginRight="8dp"
            android:inputType="textPersonName"
            android:text="Name"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp"
        style="?android:attr/buttonBarStyle" >

        <Button
            android:id="@+id/cancel_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            android:onClick="cancel"
            android:textColor="@color/white"
            style="?android:attr/buttonStyle" />

        <Button
            android:id="@+id/exit_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK"
            android:onClick="ok"
            android:textColor="@color/white"
            style="?android:attr/buttonStyle" />

    </LinearLayout>

</LinearLayout>

这就是您显示弹出窗口的方式:

private void showPopup(final Activity context) {
       LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup_layout);
       LayoutInflater layoutInflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layoutPopup = layoutInflater.inflate(R.layout.layout_popup, viewGroup);

       popup = new PopupWindow(context);
       popup.setContentView(layoutPopup);
       popup.setWidth(LayoutParams.WRAP_CONTENT);
       popup.setHeight(LayoutParams.WRAP_CONTENT);
       popup.setFocusable(true);

       popup.showAtLocation(layoutPopup, Gravity.CENTER, 0, 0);
}

您可以对弹出对话框中的按钮使用简单的 onClick 方法:

public void cancel(View v){

    Toast.makeText(getApplicationContext(), "Canceled",
            Toast.LENGTH_LONG).show();

    popup.dismiss();
}

public void ok(View v){

    Toast.makeText(getApplicationContext(), "Done",
            Toast.LENGTH_LONG).show();

    popup.dismiss();
}

您还可以阅读 this 以了解另一种创建自定义对话框的方法。