如何获取Activity的视图?

Bru*_*ges 3 java android android-alertdialog android-view

我有以下方法,我想展示一个Snackbar.问题是,我没有我的活动view,我不知道如何得到它.在代码中,你会发现Snackbar viewviewInflate_setup,但它不工作.我知道它不起作用,因为这是我的视图,alertdialog而不是我当前的屏幕.我已经尝试了root view但是然后snackbar显示在错误的地方(在这三个android按钮后面).我如何获得我的活动View

public void confirmPassword(){
        final LayoutInflater inflateSetup = getLayoutInflater();
        final View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);

        AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
        alertSetup.setView(viewInflate_setup);
        final AlertDialog dialogSetup = alertSetup.create();
        dialogSetup.show();

        btnConfirmPassowod = (Button)viewInflate_setup.findViewById(R.id.btnConfirm);
        btnConfirmPassowod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                System.out.println("User has clicked the [confirm] button");
                dialogSetup.setCancelable(false);
                edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
                SUpassword3 = edtPassword3.getText().toString();

                final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
                ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if(e==null){
                            //password confirmed, go to next setup
                            System.out.println("Password [" +SUpassword3+ "] and user [" +beforeConfirmPassword.getUsername()+ "] confirmed, go to setup");
                            dialogSetup.dismiss();
                        }else{
                            //passwords don't match
                            System.out.println("Password don't match: [" +SUpassword3 + "] USER [" +beforeConfirmPassword.getUsername()+ "]");
                            Snackbar.make(viewInflate_setup, "Wrong password. Try again.", Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                            dialogSetup.dismiss();
                        }
                    }
                });
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

Rob*_*ert 6

您唯一需要的是设置一个id根视图并创建它的一个实例。

活动_main.xml

<LineaLayout
  ...
  android:id="my_root">
  ...

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

主要活动

private LinearLayout mRootView;
onCreate(...){
  ...
  mRootView = (LinearLayout) findViewById(R.id.my_root);
  ... 
}
Run Code Online (Sandbox Code Playgroud)

而且,在您的警报对话框中,只需创建一个小吃栏,如:

Snackbar.make(mRootView, "Wrong password. Try again.", Snackbar.LENGTH_LONG).setAction("Action", null).show();
Run Code Online (Sandbox Code Playgroud)

  • `mRootView = findViewById(android.R.id.content)` 怎么样? (3认同)

Kru*_*hah 6

您可以使用findViewById(android.R.id.content)getWindow().getDecorView()获取您的活动的根视图

Snackbar.make(findViewById(android.R.id.content), "Wrong password. Try again.", Snackbar.LENGTH_LONG).show();会做的工作.