自定义对话框片段示例

时间:2015-12-31 12:37:56

标签: android android-fragments android-dialogfragment android-dialog

我很抱歉这样问。但是我在掌握如何创建自定义DialogFragment时遇到了困难。

到目前为止,我只设法创建了一个自定义对话框。这很容易做到。我们只是扩展了一个Dialog类,并使用自定义布局设置了contentview。

我想知道是否有人可以提供自定义DialogFragment的简单示例。

我正在寻找一种方法来创建一个包含多个按钮的警告对话框。

谢谢

1 个答案:

答案 0 :(得分:1)

实际上我不知道它会对你有所帮助。因为我不清楚你想要什么类型的视图。但我给你一个示例代码,以便你可以根据自己的意愿修改..我为我的申请开发了dtmf功能。更改图像和布局xml ..

//DtmfDialogFragment.java

package com.sp.zps.ui;

import com.sp.zps.R;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockDialogFragment;

public class DtmfDialogFragment extends SherlockDialogFragment {


    private static final String EXTRA_CALL_ID = "call_id";
    public static DtmfDialogFragment newInstance(int callId) {
        DtmfDialogFragment instance = new DtmfDialogFragment();
        Bundle args = new Bundle();
        args.putInt(EXTRA_CALL_ID, callId);
        instance.setArguments(args);
        return instance;
    }



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new AlertDialog.Builder(getActivity())
                .setView(getCustomView(getActivity().getLayoutInflater(), null, savedInstanceState))
                .setCancelable(true)
                .setNeutralButton("Done", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                    }
                })
                .create();
    }

    EditText dialPadTextView;
    @SuppressLint("NewApi") 
    public View getCustomView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.in_call_dialpad, container, false);
        View keyboard = v.findViewById(R.id.dialkeyboard);
        dialPadTextView = (EditText) v.findViewById(R.id.digitsText);


        ImageButton[] btns = new ImageButton[12];
        setButtonView(btns, keyboard);
        for (final ImageButton btn : btns) {
            btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialButtonPressed(v);
                }
            });
        }
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            dialPadTextView.setTextIsSelectable(true);
        }

         return v;
    }

 private void  dialButtonPressed(View v){

     String pressedKey = "";
        switch (v.getId()) {
        case R.id.button0:
            pressedKey = "0";
            break;
        case R.id.button1:
            pressedKey = "1";
            break;
        case R.id.button2:
            pressedKey = "2";
            break;
        case R.id.button3:
            pressedKey = "3";
            break;
        case R.id.button4:
            pressedKey = "4";
            break;
        case R.id.button5:
            pressedKey = "5";
            break;
        case R.id.button6:
            pressedKey = "6";
            break;
        case R.id.button7:
            pressedKey = "7";
            break;
        case R.id.button8:
            pressedKey = "8";
            break;
        case R.id.button9:
            pressedKey = "9";
            break;
        case R.id.buttonstar:
            pressedKey = "*";
            break;
        case R.id.buttonpound:
            pressedKey = "#";
            break;
        default:
            break;

        }

        udpadatePhonePad(pressedKey);
 }

 private void udpadatePhonePad(String pressedkeyVal) {

        int pos = dialPadTextView.getSelectionStart();
        dialPadTextView.getText().insert(pos, pressedkeyVal);

    }
    private void setButtonView(ImageButton[] btns, View keyboard) {

        btns[0] = (ImageButton) keyboard.findViewById(R.id.button0);
        btns[1] = (ImageButton) keyboard.findViewById(R.id.button1);
        btns[2] = (ImageButton) keyboard.findViewById(R.id.button2);
        btns[3] = (ImageButton) keyboard.findViewById(R.id.button3);
        btns[4] = (ImageButton) keyboard.findViewById(R.id.button4);
        btns[5] = (ImageButton) keyboard.findViewById(R.id.button5);
        btns[6] = (ImageButton) keyboard.findViewById(R.id.button6);
        btns[7] = (ImageButton) keyboard.findViewById(R.id.button7);
        btns[8] = (ImageButton) keyboard.findViewById(R.id.button8);
        btns[9] = (ImageButton) keyboard.findViewById(R.id.button9);
        btns[10] = (ImageButton) keyboard.findViewById(R.id.buttonstar);
        btns[11] = (ImageButton) keyboard.findViewById(R.id.buttonpound);

    }

}

像这样调用这个片段..

DtmfDialogFragment newFragment = DtmfDialogFragment.newInstance(callId);
newFragment.show(getSupportFragmentManager(), "dialog");

希望它可以帮助你找到正确的方法..