Android不会在需要时更改我的文本视图

Android不会在需要时更改我的文本视图,android,eclipse,Android,Eclipse,我需要更改文本视图,然后应用程序应该启动其他进程,但实际情况是,它完成了工作,然后继续,最后更改文本 如果有任何建议,我将不胜感激 public void onClick(View view) { setText(); work(); } 我试过了 将设置的文本代码放入“工作”代码中 在继续之前,执行if语句检查文本视图是否已更新 更多信息 在“工作”中有获取当前位置并通过电子邮件发送的代码 当用户按下按钮时,我需要在gps和电子邮件工作开始之前更改文本

我需要更改文本视图,然后应用程序应该启动其他进程,但实际情况是,它完成了工作,然后继续,最后更改文本

如果有任何建议,我将不胜感激

    public void onClick(View view) { 
    setText();
    work();
    }
我试过了

  • 将设置的文本代码放入“工作”代码中
  • 在继续之前,执行if语句检查文本视图是否已更新
更多信息

  • 在“工作”中有获取当前位置并通过电子邮件发送的代码
  • 当用户按下按钮时,我需要在gps和电子邮件工作开始之前更改文本
逗号码

public class VehicleInspection extends Activity {

    private RadioGroup radioQ1Group;
    private RadioButton radioQ1Button;
    private Button btnDisplay;
    EditText registration;
    EditText odometer;
    EditText comment;
    CheckBox Check1;
    CheckBox Check2;
    CheckBox Check3;
    CheckBox Check4;
    double latitude;
    double longitude;
    int go = 0;

    // GPSTracker Variables

        GPSTracker gps;

    @Override
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle); 
        setContentView(R.layout.activity_vehicle); 

        registration = (EditText) findViewById(R.id.editRegistration);
        odometer = (EditText) findViewById(R.id.editOdometer);
        comment = (EditText) findViewById(R.id.editComment);

        SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
        String savedReg = sharedPref.getString("name", "");
        registration.setText(savedReg);

        Check1 = (CheckBox)findViewById(R.id.checkBox1);    
        Check2 = (CheckBox)findViewById(R.id.checkBox2);    
        Check3 = (CheckBox)findViewById(R.id.checkBox3);    
        Check4 = (CheckBox)findViewById(R.id.checkBox4);    

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        Button addImage = (Button) findViewById(R.id.buttonSendVehicle); 
        addImage.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 
                    setText();
                    if (go==1)
                    {
                    work();
                    }
                                    } 
    });
    }

        public void setText()
        {
            TextView tv = (TextView)findViewById(R.id.textComment);
            tv.setText("IT WORKED!");
            go = 1;
        }

        public void work()
        {  


             AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
             Account[] list = manager.getAccounts();
             String gmail = null;
             String rating = null;
             String scheck1 = "FAIL";
             String scheck2 = "FAIL";
             String scheck3 = "FAIL";
             String scheck4 = "FAIL";
             Float ratingf = null;

             if(Check1.isChecked())
                 scheck1 = "PASS";
             if(Check2.isChecked())
                 scheck2 = "PASS";
             if(Check3.isChecked())
                 scheck3 = "PASS";
             if(Check4.isChecked())
                 scheck4 = "PASS";

             for(Account account: list)
             {
                 if(account.type.equalsIgnoreCase("com.google"))
                 {
                     gmail = account.name;
                     break;
                 }
             }

             gps = new GPSTracker(VehicleInspection.this);
             Mail m = new Mail("EMAIL@EMAIL", "); 

             String[] toArr = {"EMAIL@EMAIL"};      

             String message = ("From : " + gmail + "\n" + "Registration : " + registration.getText().toString() + "\n" + "Odometer : " + odometer.getText().toString() + "\nComment : " + comment.getText().toString()); 
             message = message + "\nTires : " + scheck1 + "\nExterior : " + scheck2 + "\nWindows : " + scheck3 + "\nInterior : " + scheck4;
             m.setFrom("EMAIL@EMAIL"); 
             m.setSubject("Yellolog : VI -" + " " + registration.getText().toString()); 
             m.setTo(toArr); 

             if(gps.canGetLocation()){

                 latitude = gps.getLatitude();
                 longitude = gps.getLongitude();

                 message = message + "\nLocation : \nLat : " + latitude + "\nLong : " + longitude;
                 m.setBody(message);

             }else{
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        }
      try { 
          //  m.addAttachment("/sdcard/filelocation"); 

            if(m.send()|| latitude != 0.0) { 
              Toast.makeText(VehicleInspection.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 

              // Create object of SharedPreferences.
              SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
             //now get Editor
              SharedPreferences.Editor editor= sharedPref.edit();
             //put your value
              editor.putString("name", registration.getText().toString());       
            //commits your edits
              editor.commit();

              odometer.setText("");
              comment.setText("");
              Check1.setChecked(false);
              Check2.setChecked(false);
              Check3.setChecked(false);
              Check4.setChecked(false);
              TextView tv = (TextView)findViewById(R.id.textComment);
              tv.setText("Comment");


            } else { 
              Toast.makeText(VehicleInspection.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
            } 
          } catch(Exception e) { 
            Toast.makeText(VehicleInspection.this, "There was a problem sending the email." + e, Toast.LENGTH_LONG).show(); 
            Log.e("MailApp", "Could not send email",e ); 
          } 
        }
    }
进步

    public void onClick(View view) { 

               tv.setText("IT WORKED!");

                if(tv.getText().toString().equals("IT WORKED!")){           
                runThread();  
              }else{
                 Toast.makeText(VehicleInspection.this, "Text Didnt change yet", Toast.LENGTH_LONG).show(); 

              }
                                } 
 });        
    }
    private void runThread() {

        new Thread() {
            public void run() {
                while (i++ < 1000) {
                    try {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {

                                work();

                            }
                        });
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
public void onClick(视图){
tv.setText(“成功了!”);
if(tv.getText().toString().equals(“它成功了!”){
runThread();
}否则{
Toast.makeText(vehicleininspect.this,“文本尚未更改”,Toast.LENGTH\u LONG.show();
}
} 
});        
}
私有void runThread(){
新线程(){
公开募捐{
而(i++<1000){
试一试{
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
工作();
}
});
睡眠(300);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}.start();
}

这会首先更改文本,然后发送电子邮件,但work()方法不会在电子邮件发送后结束

这样试试:-

        addImage.setOnClickListener(new View.OnClickListener() { 

             public void onClick(View view) { 

                           TextView tv = (TextView)findViewById(R.id.textComment);
                    tv.setText("IT WORKED!");

        if(tv.getText().toString().equals("IT WORKED!")){
Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                                      work();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });thread.start();

        }else{
    //paste code for error
    }
                                            } 
            });
你能试试这个吗

public class my_asyntask  extends AsyncTask<String, String, String> {
        TextView tv = (TextView)findViewById(R.id.textComment); 
        protected void onPreExecute() {
         //you can set first here your textview 
        }

        @Override
        protected String doInBackground(String... params) {
         //do work in background
            return null; 
        }  


        protected void onPostExecute(String string) { 
        //finally settext your textview
        }  

    }
public类my_asyntask扩展异步任务{
TextView tv=(TextView)findViewById(R.id.textComment);
受保护的void onPreExecute(){
//您可以首先在此处设置文本视图
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//在后台工作
返回null;
}  
受保护的void onPostExecute(字符串){
//最后设置文本视图
}  
}

尝试在setText()方法中调用invalidate()方法。

尝试在(i++<1000)时删除此条件,并在runOnUiThread之前使用
线程。sleep(1000)
。试着这样做:-

private void runThread() {


        new Thread() {
            public void run() {
                 try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {

                          work();

                        }
                    });
                }

        }.start();
    }

在测试“我已经尝试过”部分中所做的更改之前,您是否进行了重建?发布完整的代码!是的,我做了,它只是去完成工作中的所有代码,然后去设置文本。我需要先设定课文。你能看到我下面的答案吗?谢谢!我马上试试!它仍然只是执行工作中的代码。可能文本视图文本已更改,但不会更新屏幕上的显示或其他内容…请尝试在onCreate中初始化文本视图,然后重试!!不。它仍然发送电子邮件,然后屏幕上的文本变为“It worked”还有其他想法吗?请在调用work()方法之前延迟时间。如果需要,请增加时间谢谢您的回答!它仍然如此。我觉得代码正在运行,但在整个onclick完成之前,某些东西阻止了显示显示更改。嗨!我试过了,但没有成功。当我单击视图时,它会冻结,完成所有代码,然后应用程序会解冻,并更改textViewhi!我试过了,但没有成功。当我单击视图时,它将冻结,完成所有代码,然后应用程序将解冻,并对textView进行更改。