1

我有一个主要活动,从中调用一个片段。从片段中,我需要显示一个供用户输入的对话框。警报对话框不显示,也没有错误。使用片段中的 getActivity() 传递上下文。

这是片段中的代码部分,用于实例化包含对话框创建的帮助器类 (QuantizationHelper)

 Qradiobuttongroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
       QuantizationHelper quantizationHelper = new QuantizationHelper();     
         public void onCheckedChanged(RadioGroup group, int checkedId) {


        rssiQuantizedArray.clear();
        ProcessOutput.setText(" ");
                switch(checkedId){                  
                case R.id.radio_standardQ:
                    quantizationHelper.chooseParameters(getActivity(),checkedId,inputParams);
                    ProcessOutput.setText(quantizationHelper.StandardQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_LevelCrossing:
                    quantizationHelper.chooseParameters(getActivity(),checkedId,inputParams);
                    ProcessOutput.setText(quantizationHelper.LevelCrossingQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_Differential:   
                    ProcessOutput.setText(quantizationHelper.DifferentialQuantization(rssiArray));
                    break;

                }

            }

     });

这是带有部分代码的辅助类

public final class QuantizationHelper {

 private ArrayList<Integer> rssiQuantizedArray = new ArrayList<Integer>() ;     
 AlertDialog paramDialog;

 void chooseParameters(final Context context,final int checkedId,final ArrayList inputParams){

     AlertDialog.Builder builder = new AlertDialog.Builder(context);         

     final CharSequence[] threshholdType = {" Mean "," Median"};

    final TextView param1 = new TextView(context);  
    final EditText input1 = new EditText(context);
    final TextView param2 = new TextView(context);
    final EditText input2 = new EditText(context);

     // Set up the buttons
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         @Override
         public void onClick(DialogInterface dialog, int which) {
             switch(checkedId){ 
             case R.id.radio_LevelCrossing: 
                 inputParams.add(0,input1.getText().toString());     // Block length param 0             
                 inputParams.add(1,input2.getText().toString());  // alpha param 1

                 if (Double.valueOf((String) inputParams.get(1))>1.0 || Double.valueOf((String) inputParams.get(1))<0){
                     Toast.makeText(context, "Enter alpha in the range 0 to 1 ", Toast.LENGTH_SHORT).show();
                     return;
                 }
             }
             dialog.dismiss();
         }
     });

     switch(checkedId){                  
        case R.id.radio_standardQ:   
            builder.setTitle("Select Threshhold value");      
            builder.setSingleChoiceItems(threshholdType, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    inputParams.add(0,which);
                }

            });
            builder.setCancelable(false);
            builder.create();
            builder.show();

            break;
        case R.id.radio_LevelCrossing:   

            builder.setTitle("Choose parameters");           
            // Set up the parameter input layout
            LinearLayout dialogLayout = new LinearLayout(context);
            dialogLayout.setOrientation(1);          
            // Specify the type of input 
            input1.setInputType(InputType.TYPE_CLASS_NUMBER);
            input2.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
            param1.setText("Block Length");
            param2.setText("Alpha (0 to 1)");
            dialogLayout.addView(param1);
            dialogLayout.addView(input1);
            dialogLayout.addView(param2);
            dialogLayout.addView(input2);
            builder.setView(dialogLayout);
            builder.setCancelable(false);
            paramDialog = builder.create();
            paramDialog.show();             

            break;
        case R.id.radio_Differential:      

            break;

        }

 }

}

编辑

我已经用对话框片段尝试过这个..但仍然没有出现对话框。

Qradiobuttongroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
QuantizationHelper quantizationHelper = new QuantizationHelper();    
         public void onCheckedChanged(RadioGroup group, int checkedId) {


                rssiQuantizedArray.clear();
                ProcessOutput.setText(" ");
                switch(checkedId){                  
                case R.id.radio_standardQ:                      
                    showDialog(checkedId);
                    ProcessOutput.setText(quantizationHelper.StandardQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_LevelCrossing:
                    showDialog(checkedId);
                    ProcessOutput.setText(quantizationHelper.LevelCrossingQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_Differential:   
                    ProcessOutput.setText(quantizationHelper.DifferentialQuantization(rssiArray));
                    break;

                }

            }

     });

void showDialog(int choice) {
     DialogFragment newFragment = chooseParametersDialog.newInstance(choice);
     newFragment.setTargetFragment(this, choice);
     newFragment.show(getFragmentManager(), "dialog");
 }

 @SuppressWarnings("unchecked")
@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
     switch(requestCode) {
     case R.id.radio_standardQ:
         inputParams.add(0, data.getIntExtra("which",0));
         break;

     case R.id.radio_LevelCrossing:
         inputParams.add(0, data.getIntExtra("blocklength",3));
         inputParams.add(0, data.getDoubleExtra("alpha", 0.2));
         break;

     }
 }

这是对话片段。

public class chooseParametersDialog extends DialogFragment {

static chooseParametersDialog newInstance(int qMethod) {
    chooseParametersDialog f = new chooseParametersDialog();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("qMethod", qMethod);
    f.setArguments(args);

    return f;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int qMethod = getArguments().getInt("qMethod");
    AlertDialog.Builder builder;
    AlertDialog a = null;
    final Intent i = new Intent();

    switch(qMethod){
    case R.id.radio_standardQ:
        final CharSequence[] threshholdType = {" Mean "," Median"};  
        builder = new AlertDialog.Builder(getActivity());               
        builder.setTitle("Select Threshhold value");
        builder.setSingleChoiceItems(threshholdType, -1, new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {

                 i.putExtra("which",which); 
                 dialog.dismiss();
             }

         });
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                getTargetFragment().onActivityResult(getTargetRequestCode(),1, i);
            }
        });
        builder.setCancelable(false);
        builder.create();
        a = builder.create();
        return a;



    case R.id.radio_LevelCrossing:
        final TextView param1 = new TextView(getActivity());    
        final EditText input1 = new EditText(getActivity());
        final TextView param2 = new TextView(getActivity());
        final EditText input2 = new EditText(getActivity());

        LinearLayout dialogLayout = new LinearLayout(getActivity());
        dialogLayout.setOrientation(1);          
        // Specify the type of input 
        input1.setInputType(InputType.TYPE_CLASS_NUMBER);
        input2.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        param1.setText("Block Length");
        param2.setText("Alpha (0 to 1)");
        dialogLayout.addView(param1);
        dialogLayout.addView(input1);
        dialogLayout.addView(param2);
        dialogLayout.addView(input2);

        builder = new AlertDialog.Builder(getActivity());         
        builder.setTitle("Choose parameters");
        builder.setView(dialogLayout);
        builder.setCancelable(false);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String blocklength = input1.getText().toString();
                String alpha = input2.getText().toString();
                i.putExtra("blocklength",blocklength);
                i.putExtra("alpha",alpha);

                if (Double.valueOf((String)alpha)>1.0 || Double.valueOf((String)alpha)<0){
                    Toast.makeText(getActivity(), "Enter alpha in the range 0 to 1 ", Toast.LENGTH_SHORT).show();
                    return;

                }
                dialog.dismiss();
            }
        });
        builder.setCancelable(false);
        a = builder.create();
        return a;


    }
    return a;   
}
}

通过 .show() 代码进行调试时没有异常/错误。崩溃是因为必须与对话框一起输入的 inputparams arraylist 为空。令人沮丧!对不起,很长的帖子

4

1 回答 1

0

使用 show(FragmentTransaction transaction, String tag) 而不是 show(FragmentManager manager, String tag)

FragmentTransaction ft = getFragmentManager().beginTransaction(); newFragment.show(ft, "对话");

于 2013-09-02T13:26:38.347 回答