在Dialog Fragment中拥有自己的布局

时间:2014-02-17 16:42:13

标签: android android-fragments alertdialog

好吧,我正在测试“Dialog Fragment”并且我做了一个小程序,当它开始显示带有3个选项的Dialog Fragment时,主要活动上的TextView将显示选择了哪个选项。所以基本上它是Dialog和Activity之间的通信。我正在关注一个带有2个按钮(正面和负面)的示例,但现在我正在测试我自己的布局,带有3个按钮,我不知道如何继续......

让我们看看代码:

dialog_layout.xml中的3个按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3" />

</LinearLayout>

然后是DialogFragment类

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.webkit.WebView.FindListener;
import android.widget.Button;

public class TwoActionButtonsDialog extends DialogFragment {
     private DialogListener listener;

        public interface DialogListener {
            public void onDialogOption1(DialogFragment dialog);

            public void onDialogOption2(DialogFragment dialog);

            public void onDialogOption3(DialogFragment dialog);
        }

        // Override the Fragment.onAttach() method to instantiate the
        // NoticeDialogListener
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            // Verify that the host activity implements the callback interface
            try {
                // Instantiate the NoticeDialogListener so we can send events to the
                // host
                listener = (DialogListener) activity;
            } catch (ClassCastException e) {
                // The activity doesn't implement the interface, throw exception
                throw new ClassCastException(activity.toString()
                        + " must implement DialogListener");
            }
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            // Use the Builder class for convenient dialog construction
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            LayoutInflater inflater = getActivity().getLayoutInflater();

            builder.setTitle("Test").setView(
                    inflater.inflate(R.layout.dialog_layout, null));

           Button btn1 = ((Button) findViewById(R.id.button1));
           Button btn2 = (Button) findViewById(R.id.button2);
           Button btn3 = (Button) findViewById(R.id.button3);

            // Create the AlertDialog object and return it
            return builder.create();
        }

}

主要活动

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements
        TwoActionButtonsDialog.DialogListener {



    private static final String TAG = "dialog";
    private TextView texto = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        texto = (TextView) findViewById(R.id.textView1);
        showTwoActionButton();
    }

    public void showTwoActionButton() {
        DialogFragment dialog = new TwoActionButtonsDialog();
        dialog.show(getSupportFragmentManager(), TAG);
    }

    // The dialog fragment receives a reference to this Activity through the
    // Fragment.onAttach() callback, which it uses to call the following methods
    // defined by the NoticeDialogFragment.NoticeDialogListener interface
    @Override
    public void onDialogOption1(DialogFragment dialog) {
        // User touched the dialog's positive button
       texto.setText("1");
    }

    @Override
    public void onDialogOption2(DialogFragment dialog) {
        // User touched the dialog's negative button
         texto.setText("2");
    }

    @Override
    public void onDialogOption3(DialogFragment dialog) {
        // TODO Auto-generated method stub
        texto.setText("3");
    }

}

我不知道如何管理按钮,因为我不能这样做:

Button btn1 = ((Button) findViewById(R.id.button1));
           Button btn2 = (Button) findViewById(R.id.button2);
           Button btn3 = (Button) findViewById(R.id.button3);

它抛出一个错误:“方法findViewById(int)未定义类型TwoActionButtonsDialog”。如果我为布局充气,为什么我无法访问它们?

我该怎么办?

1 个答案:

答案 0 :(得分:1)

在为对话框浏览视图后,保存结果:

LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null)
builder.setTitle("Test").setView(v);

之后,您可以遍历视图中的按钮:

Button btn1 = (Button) v.findViewById(R.id.button1);