如何将我的警报对话框设置为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();
}
});
}
}
答案 0 :(得分:1)
这是一个使用自定义xml布局制作弹出式对话框的简单方法,它肯定会有效:
为对话框创建一个xml文件。设置android:layout_width="wrap_content"
和android:layout_height="wrap_content"
或任何其他尺寸。您还可以为布局设置背景。
例如,这是弹出窗口的xml布局:
<?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了解另一种创建自定义对话框的方法。
答案 1 :(得分:0)
为什么不能使用对话框?
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();
答案 2 :(得分:0)
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");
答案 3 :(得分:0)
而不是:
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);