java 如何获得Activity的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38664192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Activity's view?
提问by Bruno Brand?o Borges
I have the following method and I want to show a Snackbar
. The problem is, I don't have my activity view
and I don't know how to get it. In the code you'll see that Snackbar view
is viewInflate_setup
but it doesn't work. I know it doesn't work because this is the view from my alertdialog
and not my current screen. I've tried getting the root view
but then the snackbar
is shown in the wrong place(Behind those three android buttons). How do I get my activty View
?
我有以下方法,我想显示一个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();
}
}
});
}
});
}
回答by Krupal Shah
well you can use findViewById(android.R.id.content)
or getWindow().getDecorView()
to get root view of your activity
好吧,您可以使用findViewById(android.R.id.content)
或getWindow().getDecorView()
获取活动的根视图
i.e Snackbar.make(findViewById(android.R.id.content), "Wrong password. Try again.", Snackbar.LENGTH_LONG).show();
will do the job.
即Snackbar.make(findViewById(android.R.id.content), "Wrong password. Try again.", Snackbar.LENGTH_LONG).show();
会做的工作。
回答by Robert
The only thing that you need is set an id
of you root view and create an instance of it.
您唯一需要的是设置一个id
根视图并创建它的一个实例。
activity_main.xml
活动_main.xml
<LineaLayout
...
android:id="my_root">
...
</LinearLayout>
MainActivity
主要活动
private LinearLayout mRootView;
onCreate(...){
...
mRootView = (LinearLayout) findViewById(R.id.my_root);
...
}
And, in you alert dialog just create a snackbar like:
而且,在您的警报对话框中,只需创建一个小吃店,如:
Snackbar.make(mRootView, "Wrong password. Try again.", Snackbar.LENGTH_LONG).setAction("Action", null).show();