【发布时间】:2021-01-30 04:21:51
【问题描述】:
我有想要添加条件 AlertDialog 的活动代码。
我对 Android 开发太陌生了,不知道这个“常规”代码是否符合“UI”代码的条件,但是 onCreate() 有一行代码:
**setContentView(R.layout.plateform);**
无论如何,我希望“常规”代码在执行过程中等待/暂停,直到用户单击其中一个 AlertDialog 按钮。
目前,AlertDialog 可以正常启动并显示在屏幕上。
但它只在那里停留大约 2 秒,然后在没有任何用户输入的情况下消失
然后代码继续运行,就好像 AlertDialog 从未出现过一样。
我需要在后面的“常规”代码中使用该用户输入。
我怎样才能让它工作?
以下是插入新代码后跟“常规”代码的代码:
private void EnterClick() {
EditText plate = (EditText) findViewById(R.id.editTextPlate);
String newString = plate.getText().toString();
// ======= **NEW Code** ==============
if (!newString.equals("")) {
// Check If Plate PAID status should be checked at ClientSite
String UseClientChk = WorkingStorage.GetCharVal(Defines.ClientFlag, getApplicationContext());
// If appropriate, check Plate number @ Client
if (UseClientChk.equals("YES")) {
newString = checkClient(newString);
}
}
// =========================================
// Below here 'regular' code executes fine 'as is'
if (!newString.equals("")) {
// Other 'Regular' code here....
}
新的 CheckClient 代码:
protected String checkClient(String plate) {
// Make Web Service call to the Client's Website
final String ClientChkVehicleURL = "https://dlskfjdslfkjasdlkfjsdlfk/vehicle/"; // DEVELOPMENT URL
String PlateStatus = "";
String ReturnMessage = "";
ReturnMessage = HTTPFileTransfer.HTTPGetPageContent(ClientChkVehicleURL + plate , getApplicationContext());
try{
ReturnMessage = ReturnMessage.toString();
if (ReturnMessage.length() > 0) {
JSONObject obj = new JSONObject(ReturnMessage);
//String mThisDeviceName = (obj.getString("DeviceId")); // Code Model
}
} catch (JSONException e) {
//Log.d(TAG, "ERROR: " + e, e);
}
if (PlateStatus.equals("PAID")) {
// IF PAID, pop AlertDialog "Continue" or "Abort"
processingAlert = true;
PopIt("ClientSite","PAID at ClientSite");
} else if (PlateStatus.equals("NOTPAID")) {
// IF NOT PAID, AlertDialog "Not Paid at ClientSite"
processingAlert = true;
PopIt("ClientSite","NOT PAID at ClientSite");
} else {
Toast.makeText(getApplicationContext(), "Plate: " + plate.trim() + " Not Found at ClientSite", Toast.LENGTH_LONG).show();
}
return plate;
}
最后是新的 AlertDialog 代码:
以下只是测试代码,以确保一切正常。
public void PopIt( String title, String message ){
// Set Flag to Stay In Alert Window
String positiveBtnText = "";
String negativeBtnText = "";
if (! WorkingStorage.GetCharVal(Defines.LanguageType,getApplicationContext()).equals("SPANISH")) {
// ENGLISH
positiveBtnText = "CONTINUE";
negativeBtnText = "ABORT";
} else {
// SPANISH
positiveBtnText = "CONTINUAR";
negativeBtnText = "ANULAR";
}
new AlertDialog.Builder(this)
.setTitle( title)
.setMessage(message)
.setPositiveButton(positiveBtnText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of POSITIVE
Toast.makeText(getApplicationContext(), "You clicked on POSITIVE", Toast.LENGTH_LONG).show();
processingAlert = false;
}
})
.setNegativeButton(negativeBtnText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of NEGATIVE
Toast.makeText(getApplicationContext(), "You clicked on NEGATIVE", Toast.LENGTH_LONG).show();
processingAlert = false;
}
}).show();
}
以上所有代码都在同一个平台类中
如何让 AlertDialog 保持在屏幕上并等待用户输入,然后再继续执行“常规”代码?
【问题讨论】:
标签: android android-alertdialog