2016-07-29 82 views
2

我有以下方法,我想顯示一個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(); 
         } 
        } 
       }); 
      } 
     }); 
    } 
+1

這種方法的類是什麼類型? –

+1

我假設這是一個你已經寫了這個函數的活動。在這種情況下,您可以在您的activity.xml文件的根佈局中添加一個'android:id'屬性,然後使用'findViewById(R.id.root_layout_id)'來獲取視圖。你也可以通過'findViewById(android.R.id.content)'獲得你的活動的根視圖。 – wanpanman

+0

這就是我做@wanpanman。謝謝。 –

回答

4

,你需要的僅僅是設置你id根視圖,並創建它的一個實例。

activity_main.xml中

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

</LinearLayout> 

MainActivity

private LinearLayout mRootView; 
onCreate(...){ 
    ... 
    mRootView = (LinearLayout) findViewById(R.id.my_root); 
    ... 
} 

而且,你警告對話框只需創建一個小吃吧,如:

Snackbar.make(mRootView, "Wrong password. Try again.", Snackbar.LENGTH_LONG).setAction("Action", null).show(); 
+3

'mRootView = findViewById(android.R.id.content)''? –

+0

我不知道,男人!它真的很棒。謝謝! – Robert

+1

對這篇文章的所有答案的作品。感謝大家。 –

5

也可以使用findViewById(android.R.id.content)getWindow().getDecorView()以獲得您的活動的根視圖

即即Snackbar.make(findViewById(android.R.id.content), "Wrong password. Try again.", Snackbar.LENGTH_LONG).show();將執行此項工作。

+0

你說得對。謝謝。 –