在对话框中更改变量时在主活动中调用方法

时间:2018-02-01 19:20:40

标签: java android dialog

我已经列出了下面的代码,我想要实现的是从对话框(dialog2)更新的int i。然后,我想检查主活动是否已更改,如果已更改,则调用方法。我该怎么做?

主要活动

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int i = 0;


    Dialog1 dialog1 = new Dialog1(this, i);
    dialog1.show();
}

//Want to call this method whenever I is modified
private void iModified(){

}
}

Dialog 1 Class(此对话框只将其传递给dialog2)

public class Dialog1 extends Dialog{

int integerI;
Button button;


public Dialog1(final Activity activity, final int i){
    super(activity);
    setContentView(R.layout.dialog1);
    integerI = i;

    button = findViewById(R.id.dialog1Button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog2 dialog2 = new Dialog2(activity ,integerI);
            dialog2.show();
            closeDialog();
        }
    });

}

private void closeDialog(){
    this.dismiss();
}

}

对话框2:这是整数将被更改的位置,然后我希望将其发送到主活动并检查它是否已更改,如果是,则将旧整数替换为新整数。

public class Dialog2 extends Dialog {

int newI;
Button button;

public Dialog2(Activity activity, int i) {
    super(activity);
    setContentView(R.layout.dialog2);
    newI= i + 12345;


    button = findViewById(R.id.dialog2Button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            close();

                }
    });


}

private void close(){
    this.dismiss();
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用界面。

您的活动:

 public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int i = 0;

    Dialog1 dialog1 = new Dialog1(this, i, new ModifiedListener() {
        @Override
        public void notify(int i) {
            iModified();
        }
    });
    dialog1.show();
}

//Want to call this method whenever I is modified
private void iModified(){

    }
}

你的对话1:

 public class Dialog1 extends Dialog {

    int integerI;
    Button button;


    public Dialog1(final Activity activity, final int i, final ModifiedListener listener) {
        super(activity);
        setContentView(R.layout.dialog1);
        integerI = i;

        button = findViewById(R.id.dialog1Button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog2 dialog2 = new Dialog2(activity, integerI);
                dialog2.show();
                closeDialog();
            }
        });

    }

    private void closeDialog() {
        this.dismiss();
    }
}

你的对话2:

 public class Dialog2 extends Dialog {

        int newI;
        Button button;

        public Dialog2(Activity activity, int i, final ModifiedListener listener) {
            super(activity);
            setContentView(R.layout.dialog2);

            newI = i + 12345;
            //when you modify int you have to call
            listener.notify(newI);


            button = findViewById(R.id.dialog2Button);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    close();

                }
            });


        }

        private void close() {
            this.dismiss();
        }
    }

定义类似于类的接口:

public interface ModifiedListener {
    public void notify(int i);
}