D
D
Denis Afonin2014-11-05 19:52:26
Android
Denis Afonin, 2014-11-05 19:52:26

How to call different DialogFragments from one activity?

There are activities:

import android.app.Activity;
        import android.app.DialogFragment;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import java.text.ParseException;
        import java.text.SimpleDateFormat;
        import java.util.Date;

public class TwoActivity extends Activity implements View.OnClickListener, timechange.OnCompleteListener, distchange.OnCompleteListener, speedchange.OnCompleteListener {


    Button kvbutton, positionbutton, rdbutton, kvnorma, kvdistance, kvspeed;
    int kvbuttonstatus, positionbuttonstatus, rdbuttonstatus, dlg;
    Date date;
    String dist, speed;
    double dspeed, ddist;




    DialogFragment dlgtimechange;
    DialogFragment dlgdistchange;
    DialogFragment dlgspeedchange;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);

        kvbutton = (Button) findViewById(R.id.kvbutton);
        kvbutton.setOnClickListener(this);

        positionbutton = (Button) findViewById(R.id.positionbutton);
        positionbutton.setOnClickListener(this);

        rdbutton = (Button) findViewById(R.id.rdbutton);
        rdbutton.setOnClickListener(this);

        kvnorma = (Button) findViewById(R.id.kvnorma);
        kvnorma.setOnClickListener(this);

        kvdistance = (Button) findViewById(R.id.kvdistance);
        kvdistance.setOnClickListener(this);

        kvspeed = (Button) findViewById(R.id.kvspeed);
        kvspeed.setOnClickListener(this);


        dlgtimechange = new timechange();
        dlgdistchange = new distchange();
        dlgspeedchange = new speedchange();



    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.kvbutton:
                if (kvbuttonstatus==0) {
                    kvbutton.setBackgroundColor(getResources().getColor(R.color.Green));
                    kvbutton.setText("КВ on");
                    kvbuttonstatus = 1;}
                else {
                    kvbutton.setBackgroundColor(getResources().getColor(R.color.Gray));
                    kvbutton.setText("КВ off");
                    kvbuttonstatus = 0;
                    }
            break;
            case R.id.positionbutton:
                if (positionbuttonstatus==0) {
                    positionbutton.setBackgroundColor(getResources().getColor(R.color.Green));
                    positionbuttonstatus = 1;}
                else {
                    positionbutton.setBackgroundColor(getResources().getColor(R.color.Gray));
                    positionbuttonstatus = 0;
                }
                break;
            case R.id.rdbutton:
                if (rdbuttonstatus==0) {
                    rdbutton.setBackgroundColor(getResources().getColor(R.color.Green));
                    rdbutton.setText("РД on");
                    rdbuttonstatus = 1;}
                else {
                    rdbutton.setBackgroundColor(getResources().getColor(R.color.Gray));
                    rdbutton.setText("РД off");
                    rdbuttonstatus = 0;
                }
                break;
            case R.id.kvnorma:
                dlgtimechange.show(getFragmentManager(),"dlgtimechange");
                dlg = 1;
                break;
            case R.id.kvdistance:
                dlgdistchange.show(getFragmentManager(),"dlgdistchsnge");
                dlg = 2;
                break;
            case R.id.kvspeed:
                dlgspeedchange.show(getFragmentManager(),"dlgspeedchange");
                dlg = 3;
                break;
        }

    }

    @Override
    public void onComplete(String time) {
        switch (dlg){
            case 1:
                if (time.length() !=0) {
                    kvnorma.setText(time);
                    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                    try {
                        //Получаем время которое ввели в поле ввода
                        date = format.parse(time);

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    //Если уже ввели дистанцию, то вычисляем среднюю скорость
                    if (dist!=""||speed==""){
                        dspeed = Double.valueOf(dist)/Double.valueOf(date.getTime());
                    }
                }
                break;
            case 2:
                dist="";
                if (time.length() !=0) {
                    //Находим вхождение знака точка
                    int q = time.indexOf(".");
                    String subs = time.substring(q + 1);
                    //Если знак не найден
                    if (q == -1) {
                        dist = time + ".000";
                    } else {
                        if (subs.length() == 1) {
                            dist = time + "00";
                        }
                        if (subs.length() == 2) {
                            dist = time + "0";
                        }
                        if (subs.length()==3){
                            dist = time;
                        }
                    }
                    kvdistance.setText(dist);
                }
                break;
            case 3:
                if (time.length() !=0) {
                    kvspeed.setText(time);}
                break;
        }

    }
}

From it on the buttons there is a call to different dialogs. Here is an example of one of them, the rest are exactly the same:
import android.app.Activity;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;



public class timechange extends DialogFragment implements OnClickListener {

    private EditText mEditText;
    final String LOG_TAG = "myLogs";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().setTitle("Норма времени КВ, ЧЧ:ММ:СС");
        View v = inflater.inflate(R.layout.timechange, null);
        v.findViewById(R.id.btnYes).setOnClickListener(this);
        v.findViewById(R.id.btnNo).setOnClickListener(this);
        v.findViewById(R.id.btnMaybe).setOnClickListener(this);
        mEditText = (EditText) v.findViewById(R.id.editText);
        return v;

    }

    public static interface OnCompleteListener {
        public abstract void onComplete(String time);

    }

    private OnCompleteListener mListener;

    public void onAttach(Activity activity) {
        try {
            this.mListener = (OnCompleteListener)activity;
            super.onAttach(activity);
        }
        catch (final ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
        }
    }

    public void onClick(View v) {
        Log.d(LOG_TAG, "Dialog 1: " + ((Button) v).getText());
        switch (v.getId()){
            case R.id.btnYes:
                mListener.onComplete(mEditText.getText().toString());
                dismiss();
                break;
            case R.id.btnNo:
                mEditText.setText("");
                break;
            case R.id.btnMaybe:
                dismiss();
                break;
        }
        mListener.onComplete(mEditText.getText().toString());

    }

    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        Log.d(LOG_TAG, "Dialog 1: onDismiss");
    }

    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        Log.d(LOG_TAG, "Dialog 1: onCancel");
    }
}

How to separate the handlers of each of the dialogs in the activity?
How to call the same dialog with different buttons in one activity, and transfer the value received in the dialog to the calling button?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Yakushev, 2014-11-05
@VYakushev

We take the interface into a separate file:

public interface OnCompliteListener {
  public abstract void onComplete(String time);
}

We make changes to the Fragment by passing a link to the handler to the class constructor:
public class Timechange extends DialogFragment implements OnClickListener {

    private OnCompliteListener mListener;

    public Timechange(OnCompliteListener listener) {
        this.mListener = listener;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //... code
    }

    public void onClick(View v) {
        mListener.onComplete(mEditText.getText().toString());
    }
}

Well, the finishing touches to Activity. Initialize Fragments with unique handlers:
public class TwoActivity extends Activity implements View.OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);

        //... code

        dlgtimechange = new timechange(new OnCompliteListener() {
            @Override
            public onComplite(String time) {
                //... code
            }
        });
        
        dlgdistchange = new distchange(new OnCompliteListener() {
            @Override
            public onComplite(String time) {
                //... code
            }
        });
        
        dlgspeedchange = new speedchange(new OnCompliteListener() {
            @Override
            public onComplite(String time) {
                //... code
            }
        });
    }

    @Override
    public void onClick(View view) {
                //... code
    }
}

Cut out all unnecessary code, imports, etc. to make the code more descriptive

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question