我正在开发一个应用程序,我想打开自己的自定义对话框,如果我正在使用自己的自定义警告对话框,我该如何实现它呢?
enter code here
scene.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CharSequence[] items = {"auto", "night"};
AlertDialog.Builder builder = new AlertDialog.Builder(ProActivity.this);
builder.setTitle("Scene Mode");
//builder.setTitle("Timer");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("auto")) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ProActivity.this);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("preference_scene_mode", "auto");
edit.commit();
} else if (items[item].equals("night")) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ProActivity.this);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("preference_scene_mode", "night");
edit.commit();
}
}
});
builder.show();
}
});
答案 0 :(得分:1)
<强> customdialog.xml 强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/alert_corner">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView"
android:text="\u20B9"
android:textColor="@color/White"
android:textAlignment="center"
android:layout_gravity="center"
android:textSize="@dimen/font_size_normal"
android:layout_marginLeft="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@id/EDTFuelAmount"
android:background="@drawable/edittext_corner"
android:padding="10dp"
android:hint="\u20B9"
android:textAlignment="center"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="15dp" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@id/BtnFuelAmount"
android:background="@drawable/button_corner"
android:textColor="@color/White" />
</LinearLayout>
如果要打开自定义对话框,请在按钮点击事件中写下此代码
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.customdialog, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptsView);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
//custom dialog controls
TextView dialogTextView = (TextView) promptsView.findViewById(R.id.TVMsg);
Button dialogButton = (Button) promptsView.findViewById(R.id.Btn);
BtnPick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.cancel();
}
});
alertDialog.show();
答案 1 :(得分:0)
<强> MainActivity.class 强>
btn1.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DiscountDialog();
});
private void DiscountDialog() {
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_discount);
final EditText etDiscount = (EditText) dialog
.findViewById(R.id.dialog_discount_editDiscount);
dialog.findViewById(R.id.dialog_discount_CANCEL).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
dialog.findViewById(R.id.dialog_discount_OK).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
});
dialog.show();
}
<强> mainactivity.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/subtitle" >
<LinearLayout
android:id="@+id/dialog_pos_editnumber_EDITORCONTAINER"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/layer_list_pos_editnumber_dialogcontainer"
android:orientation="vertical"
android:paddingTop="0dp" >
<com.mobilepaymentspecialists.controls.FEditText
android:id="@+id/dialog_discount_editDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/layerlist_custom_edittext"
android:hint="@string/fragment_pos_DISCOUNT_HINT"
android:inputType="numberDecimal"
android:digits=".0123456789"
android:singleLine="true"
android:minHeight="40dp"
android:padding="10dp"
android:textColorHint="@color/subtitle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<com.mobilepaymentspecialists.controls.FButton
android:id="@+id/dialog_discount_OK"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/selector_green_button"
android:text="@android:string/ok"
android:textColor="@color/white"
android:textStyle="bold" />
<com.mobilepaymentspecialists.controls.FButton
android:id="@+id/dialog_discount_CANCEL"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/selector_green_button"
android:text="@android:string/cancel"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
答案 2 :(得分:0)
在style.xml中添加以下主题 - !&gt;
<style name="Theme_Dialog" parent="android:Theme.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
在 onCreateDialog 方法中使用此主题,如下所示:
Dialog dialog = new Dialog(FlightBookActivity.this,R.style.Theme_Dialog);
在xml文件中定义包含标题栏的对话框布局,并将xml文件设置为:
dialog.setContentView(R.layout.your_dialog_layout);