自定义对话框中的按钮没有功能

时间:2017-06-09 16:13:33

标签: android xml

我遇到与customdialog相关的问题。自定义对话框中的按钮没有任何功能。 setting_dialog.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:background="@color/exitdialog_background">
<TextView
    android:id="@+id/setting_title"
    android:layout_height="32dp"
    android:layout_width="match_parent"
    android:text="Setting"
    android:textSize="20dp"
    android:textColor="@color/While"
    android:paddingTop="4dp"
    android:background="#ff40c4ff"
    android:typeface="sans"
    android:textStyle="bold"
    android:gravity="center" />
<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_below="@+id/setting_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp">
        <Switch
            android:id="@+id/volume"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:checked="true"
            android:textSize="10sp" />
        <TextView
            android:id="@+id/txtVolume"
            android:layout_width="match_parent"
            android:layout_height="32dp"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:text="Volume"
            android:textColor="@color/While"/>
    </LinearLayout>
</RelativeLayout>
<LinearLayout
    android:id="@+id/linearLayoutBtns"
    android:layout_below="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp">
    <Button
        android:id="@+id/btnSave"
        android:layout_width="125dp"
        android:layout_height="35dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        android:background="@drawable/button_selector"
        android:gravity="center"
        android:text="Save"
        android:textColor="@color/While"
        android:textSize="14dp"
        android:textStyle="bold"
        android:typeface="sans" />
</LinearLayout>

设定班级

public class SettingDialog extends Dialog implements View.OnClickListener
{
    private Activity c;
    private Button btnSave;
    private TextView txtTitle;
    private int layoutResID;
    private OnSettingDialogClickListener mSaveClickListener;
    public static interface OnSettingDialogClickListener {
        public void onClick(SettingDialog settingDialog);
    }
    public SettingDialog(Activity a, int layoutResID) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
        this.layoutResID = layoutResID;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(layoutResID);
        btnSave = (Button) findViewById(R.id.btnSave);
        txtTitle  = (TextView) findViewById(R.id.setting_title);
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(false);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    public void setCancelable(Boolean cancelable)
    {
        this.setCancelable(cancelable);
    }
    public void setTitle(String title) {
        this.txtTitle.setText(title);
    }
    public SettingDialog setSaveClickListener(String textName, OnSettingDialogClickListener listener) {
        mSaveClickListener = listener;
        btnSave.setText(textName);
        return this;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnSave:
                if (mSaveClickListener != null) {
                    mSaveClickListener.onClick(SettingDialog.this);
                }
                break;
            default:
                break;
        }
    }
}

呼叫设置自定义对话框

private void ShowSetting() {
        SettingDialog settingDialog = new SettingDialog(PlayActivity.this, R.layout.setting_dialog);
        settingDialog.setTitle("Setting");
        settingDialog.setSaveClickListener("Save", new SettingDialog.OnSettingDialogClickListener() {
            @Override
            public void onClick(SettingDialog sDialog) {
                LogHelper.d("PlayActivity", " Save button ");
                sDialog.dismiss();
            }
        });
        settingDialog.show();
    }

设置自定义对话框apear,然后触摸保存按钮,但它没有任何功能。

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

Woa,我刚刚找到解决方案

Mising:btnSave.setOnClickListener(this);