繁体   English   中英

通过/作为参数传递方法-ANDROID

[英]Pass a method through/as a parameter - ANDROID

我有2节课:

TelaCadastroRestaurante(扩展活动)和Metodos(不扩展活动)。

在我的第一堂课上,我有这个: http : //i.imgur.com/N0jrjc1.png

在我的第二堂课上,我有这个: http : //i.imgur.com/PimEoxr.png

所以,我想做什么? 在我的方法caixaCerteza()中,我要通过/作为参数3传递方法mandarNuvem()。


公共类TelaCadastroRestaurante扩展了活动{

private EditText nomeRestaurante, emailRestaurante, telefoneRestaurante;
private Button buttonProximo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tela_cadastro_restaurante);
    incializarComponentes();
    acaoBotoes();
}

public void incializarComponentes() {
    nomeRestaurante = (EditText) findViewById(R.id.editTextNomeRestauranteTelaCadastroRestaurante);
    emailRestaurante = (EditText) findViewById(R.id.editTextEmailRestauranteTelaCadastroRestaurante);
    telefoneRestaurante = (EditText) findViewById(R.id.editTextTelefoneRestauranteTelaCadastroRestaurante);
    buttonProximo = (Button) findViewById(R.id.buttonProximoTelaCadastroRestaurante);

}

public void acaoBotoes() {
    buttonProximo.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            pegarValores();
            callMandarNuvem();
        }
    });
}

public void pegarValores(){
    final Restaurante rest = new Restaurante();
    rest.setNomeRest(nomeRestaurante.getText().toString());
    rest.setEmailRest(emailRestaurante.getText().toString());
    rest.setTelefoneRest(Integer.parseInt(telefoneRestaurante.getText().toString()));

}

public void callMandarNuvem(){
    Metodos.caixaCerteza(TelaCadastroRestaurante.this, 
                        "Você tem certeza que deseja cadastrar o restaurante " + nomeRestaurante.getText().toString() + "?", 
                        Metodos.mandarNuvem(TelaCadastroRestaurante.this));
}

}

公共类Metodos {

private static ProgressDialog dialog;

// Metodo que mostra o Aguarde a verificação
public static void taskInProgres(boolean mostrar, Context context) {

    if (dialog == null) {
        dialog = new ProgressDialog(context);
        dialog = ProgressDialog.show(context, "","Espere um momento...", true);
    }
    if (mostrar) {
        dialog.show();
    } else {
        dialog.dismiss();
    }
}

// Metodo que mostra a caixa de certeza
public static void caixaCerteza(final Context context, final String texto, final Metodos metodo) {
    AlertDialog.Builder builderaction = new AlertDialog.Builder(context);
    builderaction.setTitle("Atenção!");
    builderaction.setMessage(texto);

    builderaction.setPositiveButton("Sim",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
    builderaction.setNegativeButton("Não",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builderaction.create();
    alert.setIcon(R.drawable.ic_stop);
    alert.show();
}


public static void mandarNuvem(final Context context){

    Metodos.taskInProgres(true, context);

        Restaurante rest = new Restaurante();

        ParseObject restauranteParse = new ParseObject("Restaurante");
        restauranteParse.put("nomeRestaurante", rest.getNomeRest());
        restauranteParse.put("emailRestaurante", rest.getEmailRest());
        restauranteParse.put("telefoneRestaurante", rest.getTelefoneRest());
        restauranteParse.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Toast.makeText(context,"Salvo com sucesso!", Toast.LENGTH_SHORT).show();
                    Metodos.taskInProgres(false, context);
                } else {
                    Toast.makeText(context, e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        });
}

您不能传递方法,但可以传递包含方法的对象。 这意味着可能会有更多的代码。 用类似的方式致电caixaCerteza

caixaCerteza(..., ..., new Callable() {
    @Override
    public void call() {
        mandarNuvem();
    }
});

在方法caixaCerteza(..., ..., Callable callable) ,使用

callable.call();

编辑:

如果可以将mandarNuvem方法放在实现某些接口的类中/甚至可以扩展一个超类(例如Callable ),然后caixaCerteza直接用作caixaCerteza的第三个参数(而不是将其包装在匿名Callable对象中),则可以caixaCerteza此过程。 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM