如何获取Activity的视图?
How to get Activity's view?
我有下面的方法,我想显示一个Snackbar
。问题是,我没有 activity 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();
}
}
});
}
});
}
您唯一需要做的就是设置一个 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();
您可以使用 findViewById(android.R.id.content)
或 getWindow().getDecorView()
获取 activity
的根视图
即 Snackbar.make(findViewById(android.R.id.content), "Wrong password. Try again.", Snackbar.LENGTH_LONG).show();
将完成这项工作。
我有下面的方法,我想显示一个Snackbar
。问题是,我没有 activity 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();
}
}
});
}
});
}
您唯一需要做的就是设置一个 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();
您可以使用 findViewById(android.R.id.content)
或 getWindow().getDecorView()
获取 activity
即 Snackbar.make(findViewById(android.R.id.content), "Wrong password. Try again.", Snackbar.LENGTH_LONG).show();
将完成这项工作。