0

拥有此类代码来创建对话框:

           public class DConce extends DialogFragment{

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    dshow.setView(inflater.inflate(R.layout.dialogconc, null))

           .setPositiveButton("Send", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {

                   //TODO


               }
           })
           .setNegativeButton("Close", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   DConce.this.getDialog().cancel();
               }
           });      

    return dshow.create();

    }

           public static DConce newInstance(){
           DConce arg = new DConce();
           return arg;
            }

我正在尝试使用 EditText 从用户输入中获取一些数据。该对象是在 xml 上创建的。我试图用它来称呼它

EditText dlgText = (EditText) Dconce.findViewById(R.id.txconc);

但它只是不会在对话框中找到 findViewByid。这里不包括视图吗?dshow.setView(inflater.inflate(R.layout.dialogconc, null))

请问有什么帮助吗?提前致谢。

PS:对话框创建得很好,我可以看到 EditText。我只是无法使用它。

4

3 回答 3

1

我在对话框中夸大了我的视图,并进行了一些修改,它起作用了,如下所示:

LayoutInflater adbInflater = LayoutInflater.from(getActivity());
        View editTextLayout = adbInflater.inflate(R.layout.dialogconc, null);
        EditText dlgText = (EditText ) editTextLayout .findViewById(R.id.txconc);

为我工作:

public class DConce extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());

    LayoutInflater adbInflater = LayoutInflater.from(getActivity());
    final View editTextLayout = adbInflater.inflate(R.layout.edittext_layout, null);


    dshow.setView(editTextLayout)

           .setPositiveButton("Send", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   EditText dlgText = (EditText)editTextLayout.findViewById(R.id.editText1);
                   String string = dlgText.getText().toString();
                   Log.d("sadsad", string);
               }
           })
           .setNegativeButton("Close", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   DConce.this.getDialog().cancel();
               }
           });      

    return dshow.create();

    }

           public static DConce newInstance(){
               DConce arg = new DConce();
           return arg;
            }
}

调用活动:

FragmentManager manager = getSupportFragmentManager(); 
                DialogFragment Dialog = DConce.newInstance();
                Dialog.show(manager, "tag");
于 2013-04-04T08:52:20.323 回答
1

你应该试试EditText dlgText = (EditText) dshow.findViewById(R.id.txconc);

于 2013-04-04T08:57:02.157 回答
1
AlertDialog.Builder editalert = new AlertDialog.Builder(ActivityName.this);
        editalert.setTitle("My Title");
        final EditText input = new EditText(ActivityName.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT);
        input.setLayoutParams(lp);
        editalert.setView(input);
        editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

           String name= input.getText().toString();

            }
        });

        editalert.show();

您还可以使用自定义布局进行对话框

你的 dialog.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:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="20dp"
    android:text="OK" />

<Button
    android:id="@+id/cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/ok"
    android:layout_alignBottom="@+id/ok"
    android:layout_marginLeft="35dp"
    android:layout_toRightOf="@+id/ok"
    android:text="CANCEL" />

   </RelativeLayout>

在您的主要活动中

    Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showpopup();
        }

    });
}
public void showpopup()
{
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("my title");
d.setContentView(R.layout.dialog);
// Thank you Button Listener. On Click Goes to Home Screen
 Button ok = (Button) d.findViewById(R.id.ok);
final EditText ed= (EditText) d.findViewById(R.id.editText1);
ok.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String s= ed.getText().toString();

    }

});
 Button cancel = (Button) d.findViewById(R.id.cancel);

    ok.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            d.cancel();

        }

    });
    d.show();

}
于 2013-04-04T09:30:55.653 回答