首页 文章

在显示时更新android对话框的ContentView而不关闭

提问于
浏览
0

我正在尝试使用带有下面代码段的 alertDialog.Builderdialog bu中显示我的自定义视图 .

AlertDialog.Builder alertViewBuilder = null;
AlertDialog alertViewDialog = null;
public void showPopupView(View popupView) {

if (alertViewBuilder == null) {
    alertViewBuilder = new AlertDialog.Builder(ctContext);
}
    alertViewBuilder.setView(popupView);

if (alertViewDialog == null)
    alertViewDialog = alertViewBuilder.create();

if (!alertViewDialog.isShowing())
    alertViewDialog.show();
}

如果用户对该自定义视图的组件执行任何操作,我想更改/更新 dialog 的内容 . 所以当用户触摸/点击自定义视图的任何组件时,我正在更新 popupView 并再次将更新的 popupView 传递给 showPopupView(View popupView) . 但视图未在 dialog 上更新 .

我的要求类似于以下示例 .

  • 在对话框中显示 usernamepassword 文本输入字段和“ OK ”按钮的登录页面 .

  • 如果用户输入了错误的用户名/密码并按“ OK ”按钮,则应使用视图更新 Dialog ,该视图将显示错误消息textview而不是 usernamepassword 文本输入字段和“ OK ”按钮应替换为 "Try Again" 按钮在同一个对话框上 .

  • 当用户按“再试一次”按钮时,上一个登录页面应显示在同一个对话框中 .

我的意思是流量应该与相同活动的变化视图相似 . 我想我已经提供了有关我的问题的充分信息 . 请建议我解决我的问题 . 提前致谢 .

1 回答

  • 0

    我能够管理并找到解决此问题的方法 . 刚删除 AlertDialog.Builder 并将 AlertDialog 更改为 Dialog 并在 Dialog 中调用addContentView(view)而不是在 AlertDialog.Builder 上调用setView(view) . 并在 Dialog 上的contentview上添加和删除chaildviews . 找到下面的代码片段 .

    private android.view.ViewGroup.LayoutParams alertLayoutParams = null;
    private Dialog alertViewDialog = null;
    private LinearLayout cvPopupView;
    
    public void showPopupView(View popupView) {
        if (cvPopupView == null)
            cvPopupView = new LinearLayout(ctContext);
        else
            cvPopupView.removeAllViews();
    
        cvPopupView.addView(popupView);
    
        if (alertViewDialog == null) {
            alertViewDialog = new Dialog(ctContext);
            alertViewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
    
        if (alertLayoutParams == null) {
            alertLayoutParams = new android.view.ViewGroup.LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    
        if (!alertViewDialog.isShowing()) {
            alertViewDialog.addContentView(cvPopupView, alertLayoutParams);
            alertViewDialog.show();
        }
    }
    

    如果任何其他人面临同样的问题,这可能会有所帮助 .

相关问题