【问题标题】:How to show loading animation in android while using Chaquopy如何在使用 Chaquopy 时在 android 中显示加载动画
【发布时间】:2021-10-13 02:26:47
【问题描述】:

我在我的应用程序中使用 python,但是当我单击按钮时,我面临加载动画的问题,比如进度条不起作用,应用程序移动到 python 代码和 java 文件卡住并且当时没有响应显示正在加载 pyhon 代码的动画,谁能帮我找出代码在下面...

        if (!Python.isStarted()) {
            Python.start(new AndroidPlatform(documentupload.this));
        }
                b2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        pgb.setVisibility(View.VISIBLE);
                        if(isConnected())
                        {



                        String strUserName = et.getText().toString();
                        String numofdocword=docnum.getText().toString();

                       if(TextUtils.isEmpty(strUserName) && TextUtils.isEmpty(numofdocword))
                       {
                            et.setError("Enter total number");
                            docnum.setError("Enter maximum number of words for document");
                            return;
                       }
                       else if(Integer.parseInt(strUserName)>100){
                           Toast.makeText(documentupload.this,"Number must be less than 100",Toast.LENGTH_SHORT).show();
                       }


                       else{
                           if(upload.length()==0) {
                               Toast.makeText(documentupload.this, "Fill the required fields", Toast.LENGTH_SHORT).show();

                           }
                           else if(adapter.getItemCount()==0){
                               Toast.makeText(documentupload.this, "Words must be present", Toast.LENGTH_SHORT).show();

                           }
                           else{
                               py = Python.getInstance();
                               final PyObject pyobj = py.getModule("count");
                               final PyObject pyspellcheck = py.getModule("grammer2");
                               final PyObject pywordsimilarity=py.getModule("forwordsimilarity");
                               final PyObject pysentencesimilarity = py.getModule("newsentencesimilarity");
                               final PyObject sencount=py.getModule("word2vec");
                               final PyObject pywordspell=py.getModule("spellcheck");



                              PyObject obj =  pyobj.callAttr("main", sf);
                               String count=obj.toString();

                               PyObject spellobjgrammer = pyspellcheck.callAttr("main", sf);
                               PyObject spellwordobj = pywordspell.callAttr("main", sf);

                               List<PyObject> wordsimilarityobj = py.getModule("forwordsimilarity").callAttr("main",sf,Words.toString()).asList();
                               String s=wordsimilarityobj.get(0).toString();
                               String res2 = wordsimilarityobj.get(1).toString();
                               String res3= wordsimilarityobj.get(2).toString();

                               List<PyObject> sentencesimilarityobj = (List<PyObject>) pysentencesimilarity.callAttr("main",sf,Definitions.toString()).asList();
                               String similarsent=sentencesimilarityobj.get(0).toString();
                               String docsentcount=sentencesimilarityobj.get(1).toString();

                               PyObject sentence_countobj = sencount.callAttr("main",Definitions.toString());
                               //Toast.makeText(documentupload.this, "uploaded" + sf, Toast.LENGTH_LONG).show();
                               //String pobj = obj.toString();
                               String grammermistake = spellobjgrammer.toString();
                               //String sentencesimilar = sentencesimilarityobj.toString();
                               String sentence_count=sentence_countobj.toString();
                               String wordspellingmistake= spellwordobj.toString();

                              // Toast.makeText(documentupload.this, res3, Toast.LENGTH_LONG).show();

                               Intent intent = new Intent(documentupload.this, result.class);
                               intent.putExtra("numberwords", count);
                               intent.putExtra("spellmistake", grammermistake);
                               intent.putExtra("defitionsimilarity", similarsent);
                               intent.putExtra("wordsimilarity",s);
                               intent.putExtra("stop",res2);
                               intent.putExtra("datacount",res3);
                               intent.putExtra("sentence_count",sentence_count);
                               intent.putExtra("totalnumber",et.getText().toString());
                               intent.putExtra("wordspelling",wordspellingmistake);
                               intent.putExtra("docsentcount",docsentcount);
                               intent.putExtra("filenumberofwords",docnum.getText().toString());
                               pgb.setVisibility(View.GONE);
                               startActivity(intent);
                               finish();

                           }

                       }


                        }
                        else {
                            Toast.makeText(documentupload.this,"No internet connection",Toast.LENGTH_SHORT).show();
                        }
                    }


                });


【问题讨论】:

    标签: java python android chaquopy


    【解决方案1】:

    进度条或任何其他动画只有在 UI 线程未阻塞时才会起作用。因此,如果您的代码需要很长时间才能运行,您将不得不在后台线程上运行代码并将 UI 更新发布到 UI 线程,如 this answer 中所述。

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2010-11-21
      • 1970-01-01
      相关资源
      最近更新 更多