当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

How can i make my alert dialogue into a custom alert in android.please help me to display my dialogue box in a custom alert. my code is pasted here.

the code contains an out put of 10 list view. and when click on item in the list it could be alerted..

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();

}

});

}

}

网友答案:

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();
}

}

To open this dialog

MyDialog.getInstance().show(getFragmentManager(), "your TAG");
网友答案:

That's a simple way to make a popup dialog with a custom xml layout, It will surely work:

Make an xml file for your dialog. Set android:layout_width="wrap_content" and android:layout_height="wrap_content" or any other size. You can also set a background for your layout.

For example this is an xml layout for a popup 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>

And that's how you show the popup:

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);
}

You can use simple onClick methods for buttons in your popup dialog:

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();
}

You can also read this for another way of creating custom dialogs.

网友答案:

Why can't you use dialog?

        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();
网友答案:

Instead of this :

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

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: