我有以下方法,我想顯示一個Snackbar
。問題是,我沒有我的活動view
,我不知道如何得到它。在代碼中,您會看到Snackbar view
是viewInflate_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();
}
}
});
}
});
}
這種方法的類是什麼類型? –
我假設這是一個你已經寫了這個函數的活動。在這種情況下,您可以在您的activity.xml文件的根佈局中添加一個'android:id'屬性,然後使用'findViewById(R.id.root_layout_id)'來獲取視圖。你也可以通過'findViewById(android.R.id.content)'獲得你的活動的根視圖。 – wanpanman
這就是我做@wanpanman。謝謝。 –