How to add edittext in snackbar in android program

2019-09-22 08:28发布

I have Snackbar in my application. I want to add Edittext in Snackbar to accept some input. How can I add an Edittext in Snackbar?

2条回答
可以哭但决不认输i
2楼-- · 2019-09-22 09:09

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);
查看更多
Rolldiameter
3楼-- · 2019-09-22 09:14
    //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();

enter image description here

查看更多
登录 后发表回答