简体   繁体   English

如何以编程方式在android中的snackbar中添加edittext?

[英]How to add edittext in snackbar in android programmatically?

I have Snackbar in my application.我的应用程序中有Snackbar I want to add Edittext in Snackbar to accept some input.我想在Snackbar添加Edittext以接受一些输入。 How can I add an Edittext in Snackbar ?如何在Snackbar添加Edittext

    //Custom layouts are discouraged due to the intended use of Snackbars,but this will do your task!
    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear_layout_root);

    final Snackbar snackbar = Snackbar.make(linearLayout, "Hey Whats Up", Snackbar.LENGTH_INDEFINITE);
    Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();

     // Inflate your custom view with an Edit Text
    LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null); // custom_snac_layout is your custom xml 

    layout.addView(snackView, 0);
    snackbar.show();

在此处输入图片说明

You need to inflate new view and add it to snackBar's layout.您需要扩充新视图并将其添加到小吃店的布局中。

// Make snackbar
Snackbar snackbar = Snackbar.make(rootView, "", Snackbar.Snackbar.LENGTH_INDEFINITE);
// Fetch the layout
Snackbar.SnackbarLayout sLayout = (Snackbar.SnackbarLayout) snackbar.getView();
// remove the default textView
TextView textView = (TextView) sLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.GONE);


// Inflate custom view (have an edittext in this)
View newView = mInflater.inflate(R.layout.edittext_layout, null);

sLayout.setView(newView,0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM