【问题标题】:android alertdialog pause activityandroid alertdialog 暂停活动
【发布时间】: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


    【解决方案1】:

    整个代码在 UI 线程上运行。

    您是否在声明后开始一个新的活动://此处的其他“常规”代码..

    我的猜测是你要么完成当前的活动,要么开始一个新的活动,这会使对话框闪烁一秒钟。

    尝试:

     AlertDialog alertDialog = 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;
                    }
                });
    
    alertDialog.show();
    alertDialog.setCancelable(false);
    

    【讨论】:

    • 我将不得不在下一个尝试您的建议。为了让事情快速运行,我最终重新构建了 Iis that a word??) 代码,以便随后的“常规”代码被放入它自己的函数中,并最终在 PositiveButton 的 OnClick() 中被调用。这导致事情正常进行。谢谢
    【解决方案2】:

    科特林 对我有用的是设置警报对话框,然后让后续代码从警报对话框中运行出来。现在,当我查看屏幕时,我看到了一个可以单击的按钮。除非我说可以继续,否则我什么都不想做,所以看起来很简单。 例如

    binding.Btn.setOnClickListener { funXDialog(A, B) }
    

    然后

     fun funXDialog(A:B) {
    
    val contextX = requireContext()
    val alert = AlertDialog.Builder(contextX)
    alert.setTitle("Title")
    alert.setMessage("message")
      
        alert.setPositiveButton("OK")) { _, 
         _ ->
            funX(A,B) // execute the function if the positive button is pressed
        }
    
        alert.setNegativeButton("Cancel") {_, _ ->
         funDoNothing() // if the cancel button is pressed.
        }
    
        alert.show()
    
    
    }}
    

    我正在处理一个片段。通过这种方式,我让用户暂停执行,直到他决定要做什么。这项工作喜欢魅力。需要注意的一件事 --- funXDialog 可以正常工作,并且它调用的函数是在调用片段中设置的,而不是在其他地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多