AlerDialog具有自定义布局

时间:2017-12-22 12:27:44

标签: android kotlin

我创建了这个布局,所以我可以使用这个alertdialog。我创建它,因为我必须在点击后保存状态,当我打开对话框时,我可以获得之前标记的值。搜索偏好。

但即便开展这项活动,我也无法获得他们的身份证明,因为他们不在活动的布局中。

如何使用此布局?

代码:

R.layout.alertdialog_radiobutton_account >> my layout

功能:

有趣的onWhoCanContactMeClicked(活动:活动){

val dialog: AlertDialog.Builder = AlertDialog.Builder(activity)
dialog.setTitle(activity.getString(R.string.whocancontact_settings))
        .setView(R.layout.alertdialog_radiobutton_account)
        .setPositiveButton(activity.getString(R.string.ok_dialog)) { p0, p1 ->
            //TODO After click set state
        }
        .setNegativeButton(activity.getString(R.string.cancel_dialog)) { p0, p1 ->
            p0.dismiss() //dismiss dialog
        }
        .create()
        .show()

}

XML:

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>

        <variable
            name="viewModel"
            type="com.plugapps.zuk.viewmodel.AccountViewModelKotlin">

        </variable>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/margin_large"
        android:orientation="vertical">

        <RadioGroup
            android:id="@+id/rdg_dialog_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_large"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/rdb1_contact"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="@string/everyone_dialog"
                android:textSize="16sp" />

            <RadioButton
                android:id="@+id/rdb2_contact"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="@string/onlymycontacts_whocancontact_settings"
                android:textSize="16sp" />

            <RadioButton
                android:id="@+id/rdb3_contact"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="@string/my_contacts"
                android:textSize="16sp" />

        </RadioGroup>
    </LinearLayout>
</layout>

布局图片:

enter image description here

4 个答案:

答案 0 :(得分:0)

您使用此API https://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(int)

我建议您自己充气dialogView,然后使用此方法 https://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(android.view.View)

然后在按钮点击回调中,您可以使用dialogView.findById

答案 1 :(得分:0)

您可以使用DialogFragment创建CustomDialogFragment。 你可以绑定你的观点。

public class CustomDialogFragment extends DialogFragment {

    public static final String TAG = "CustomDialogFragment";
    private View mRootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
        ButterKnife.bind(this, mRootView);

        return mRootView;
    }

    @OnClick(R.id.button_done)
    public void onDoneClicked() {

    }

    @OnClick(R.id.button_cancel)
    public void onCancelClicked() {
    }

}

现在进入您的MainActivity:

CustomDialogFragment  mDialog = new CustomDialogFragment();
mDialog .show(getSupportFragmentManager(), CustomDialogFragment.TAG);

答案 2 :(得分:0)

这是我改变的功能,它对我有用:

fun onWhoCanContactMeClicked(activity: Activity) {

    val li: LayoutInflater = activity.layoutInflater
    val view: View = li.inflate(R.layout.alertdialog_radiobutton_account, null)

    var rgb1: RadioGroup = view.findViewById(R.id.rdg_dialog_account)
    var rdb0: RadioButton = view.findViewById(R.id.rdb1_contact)
    var rdb1: RadioButton = view.findViewById(R.id.rdb2_contact)
    var rdb2: RadioButton = view.findViewById(R.id.rdb3_contact)

    when (whoCanContactMe) { //esta variável é um Int
        1 -> rgb1.check(rdb1.id)
        2 -> rgb1.check(rdb2.id)
        else -> rgb1.check(rdb0.id)
    }

    val dialog: AlertDialog.Builder = AlertDialog.Builder(activity)
    dialog.setTitle(activity.getString(R.string.whocancontact_settings))
            .setView(view)
            .setPositiveButton(activity.getString(R.string.ok_dialog)) { dialog, wich ->
                whoCanContactMe = when {
                    rdb1.isChecked -> 1
                    rdb2.isChecked -> 2
                    else -> 0
                }
            }
            .setNegativeButton(activity.getString(R.string.cancel_dialog)) { p0, p1 ->
                p0.dismiss() //dismiss dialog
            }
            .create()
            .show()
}

答案 3 :(得分:0)

试试这个,它为我工作

val inflater = layoutInflater

    val dialogLayout = inflater.inflate(R.layout.alert, null)
    val builder = AlertDialog.Builder(this)
    builder.setView(dialogLayout)

    val alertDialog = builder.show()
    val rb1 = alertDialog.findViewById<RadioButton>(R.id.rdb1_contact)
    rb1?.setChecked(true)