我需要文本視圖來更改,然後應用程序應該啓動其他進程,但會發生什麼,然後它會執行該操作並最後更改文本。當我需要它時,Android不會更改我的TextView
我將不勝感激任何建議!
public void onClick(View view) {
setText();
work();
}
我已經試過
- 把一套文本代碼中的 「工作」 的代碼。
- 做的,如果檢查語句,如果文本視圖繼續
更多信息
- 裏面的「工作」之前更新有用於獲取當前位置,並在電子郵件中發送它的代碼。
- 當用戶按下按鈕時,我需要在gps和電子郵件工作開始之前更改文本。
Commplete代碼
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 protected]", ");
String[] toArr = {"[email protected]"};
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 protected]");
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();
}
這將首先更改文本,併發送電子郵件,但work()方法在電子郵件發送後不會結束。
在測試從「我曾嘗試過」部分所做的更改之前,您是否重建過? – Brovoker
發佈您的完整代碼! –
是的,我做了,它只是去完成所有的代碼工作,然後去設置文本。我需要先設置的文字 – user3472145