【问题标题】:How to change the text of a TextView within an interface如何在界面中更改 TextView 的文本
【发布时间】:2020-02-05 18:21:38
【问题描述】:

大家。 实际上我正在 Android Studio 中做一个应用程序。 当我尝试更改属于对话框的 TextView 的文本时遇到问题。 我认为问题是因为我试图在接口内部做一个 setText 。 这是我的对话框。

 private void showDialogDeEspera() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.dialog_de_carga_o_espera, null, false);
    builder.setView(view);
    dialogEspera = builder.create();
    textEspera = view.findViewById(R.id.text_dialog_carga_o_espera);
    progresoPing = view.findViewById(R.id.progress_carga_o_espera);
    progresoPing.setVisibility(View.VISIBLE);
    dialogEspera.show();
}

这是我获得响应并尝试修改文本的界面。

ResponsePingWs rpw = new ResponsePingWs() {
        @Override
        public void responsePing(String error) {
            validarRespuesta(error);
        }
};

validarRespuesta 方法是您尝试修改错误文本的地方。

private void validarRespuesta(String error){
    if (error.equals("")){
        textEspera.setText("No se puede accesar al servisor");
        SystemClock.sleep(3000);
        Toast.makeText(this, "No se pudo acceder al servidor, revisar URL", Toast.LENGTH_SHORT).show();
        dialogEspera.dismiss();
    }else if (error.equals("success")){
        textEspera.setText("Coneción exitosa!");
        SystemClock.sleep(3000);
        Toast.makeText(this, "El servidor respondió correctamente!", Toast.LENGTH_SHORT).show();
        dialogEspera.dismiss();
    }else if (!error.equals("") && !errorPing.equals("success")){
        textEspera.setText(error);
        SystemClock.sleep(3000);
        Toast.makeText(this, "El servidor respondió con un mensaje de error", Toast.LENGTH_SHORT).show();
        dialogEspera.dismiss();
    }
}

如果有人有这方面的想法或解决方案,如果你能帮助我,我将不胜感激,谢谢!

【问题讨论】:

  • 您是否尝试从后台线程调用 interfacePing 方法?在android中,视图只能在主线程中修改。也许你可以尝试把 runOnUiThread.
  • 其实我是从Volley Response Listener调用我的接口的方法

标签: android interface


【解决方案1】:

谢谢@Dhaval Shah 我使用了 runOnUiThread,如果它有效!。

 final ResponsePingWs rpw = new ResponsePingWs() {
                    @Override
                    public void responsePing(String error, Context context) {
                        runThread(error, context);
                    }
                };
                pingAPI(getApplicationContext(), rpw);

这是我将要实现的 runThread 方法

private void runThread(final String error, final Context context) {

    new Thread() {
        public void run() {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (error.equals("")){
                                textEspera.setText("No se puede accesar al servisor");
                                SystemClock.sleep(3000);
                                Toast.makeText(context, "No se pudo acceder al servidor, revisar URL", Toast.LENGTH_SHORT).show();
                                dialogEspera.dismiss();
                            }else if (error.equals("success")){
                                textEspera.setText("Coneción exitosa!");
                                SystemClock.sleep(3000);
                                Toast.makeText(context, "El servidor respondió correctamente!", Toast.LENGTH_SHORT).show();
                                dialogEspera.dismiss();
                            }else if (!error.equals("") && !errorPing.equals("success")){
                                textEspera.setText(error);
                                SystemClock.sleep(3000);
                                Toast.makeText(context, "El servidor respondió con un mensaje de error", Toast.LENGTH_SHORT).show();
                                dialogEspera.dismiss();
                            }
                        }
                    });
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }.start();
}

【讨论】:

    猜你喜欢
    • 2012-05-28
    • 2011-01-19
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多