用于alertDialog的setContentView

时间:2014-04-16 02:51:08

标签: android android-layout android-alertdialog

如果有两个textEdit,则在对话框中弹出该对话框。我想从提交的文本编辑中获取信息,但不知道如何。我也不知道如何为普通对话框提交提交或取消按钮(我知道你可以在带有“setNegativeButton”的alertDialog上)。

那么如何在常规对话框中添加提交或取消?这就是我到目前为止所做的工作:

public void changeEmail(View v){

    Dialog dialog =  new Dialog(this);
    dialog.setContentView(R.layout.change_email_dialog);
    dialog.setTitle("Enter your new email");
    dialog.show();


}

我想我也想知道是否有人可以快速解释我如何从对话框所使用的布局内部的两个textEdits中获取信息?

4 个答案:

答案 0 :(得分:3)

创建一个布局文件:custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF"/>

</RelativeLayout>

单击按钮创建以下onClick函数(当您想要出现对话框时):

button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text1);
            text.setText("Text view 1");

            TextView text = (TextView) dialog.findViewById(R.id.text2);
            text.setText("Text view 2");

            dialog.show();
          }
        });

以下链接指向tutorial,以获取您想要的内容。

这是关于Stack Overflow问题的link,它提供了有关自定义对话框的信息。

答案 1 :(得分:0)

制作自己的对话

class YourDialog extends Dialog {

private TextView mTextView1, mTextView1;
private Button mButton1, mButton2;

public YourDialog(Context context) {
    super(context);


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.change_email_dialog);

    // init your component from layout
    // and add listiner to them

}

}

答案 2 :(得分:0)

     protected void showCustomDialog(final int position) {
            final Dialog dialog;
            dialog = new Dialog(getActivity());
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.alert_enter);
            EditText editText;
            EditText editText2;
            editText = (EditText)dialog.findViewById(R.id.abc);
            editText2 = (EditText)dialog.findViewById(R.id.abcd);
            buttonOK = (Button)dialog.findViewById(R.id.quantity_ok_button);
            buttonOK.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                        String text=editText.getText().toString();
                        String text2=editText2.getText().toString();
                        dialog.dismiss();
                }
            });
}

您可以尝试使用此代码。我这样做是为了从对话框中获取一个值。使用值文本和文本2.

答案 3 :(得分:0)

我一直面临着同样的障碍。

以下是我在研究后所做的事情。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="1"
tools:context="com.example.sinnaren.simply.MainActivity">
<Button
    android:id="@+id/button_add_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Add a title"
    android:textAllCaps="false"/>
</LinearLayout>

dialog_add_title_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_chapter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
    android:id="@+id/edit_add_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="title name"
    android:inputType="textCapWords"
    android:textAllCaps="false" />

<EditText
    android:id="@+id/edit_add_desc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="title description"
    android:inputType="textCapSentences"
    android:textAllCaps="false" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements myDialogFragment.MyDialogListener {
private Button buttonAdd;
private myDialogFragment dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dialog = new myDialogFragment();
    buttonAdd = (Button)findViewById(R.id.button_add_title);

    buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("MainActivity", "onClick: going to show dialog");
            dialog.show(getSupportFragmentManager(), "myCustomAlert !!");

        }
    });

}

@Override
public void onDialogPositiveClick(DialogFragment dialog, String name, String desc) {
    Toast.makeText(getApplicationContext(), "name :" + name + "description :" + desc, Toast.LENGTH_SHORT).show();
}

@Override
public void onDialogNegativeClick(DialogFragment  dialog) {
    Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
    }
}

myDialogFragment.java

public class myDialogFragment extends DialogFragment {
private Dialog alert;
MyDialogListener dialogListener;

public interface MyDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, String namel, String desc);
    public void onDialogNegativeClick(DialogFragment dialog);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    dialogListener = (MyDialogListener)context;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Log.w("onCreateDialog", "onCreateDialog: onCreateDialog invoked");
    AlertDialog.Builder alertDialogBuider = new AlertDialog.Builder(getActivity());

    alertDialogBuider.setTitle("Add a Title");

    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_add_title_list, null);
    alertDialogBuider.setView(dialogView);
    alertDialogBuider.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            EditText textChapterName = (EditText)dialogView.findViewById(R.id.edit_add_title);
            EditText textChapterDesc = (EditText)dialogView.findViewById(R.id.edit_add_desc);
            dialogListener.onDialogPositiveClick(myDialogFragment.this,
                    textChapterName.getText().toString(),
                    textChapterDesc.getText().toString());
        }
    });

    alertDialogBuider.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialogListener.onDialogNegativeClick(myDialogFragment.this);
        }
    });

    return alertDialogBuider.create();
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    Toast.makeText(getActivity(), "Dialog: onDismiss", Toast.LENGTH_SHORT).show();
    }
}

使用支持库时,请确保导入 android.support.v4.app.DialogFragment 类,而不是 android.app.DialogFragment

dialog created