如何使用LayoutInflater按钮和editText进行修改的Android Snackbar [英] How to make modification Android Snackbar containing with LayoutInflater button and editText

查看:90
本文介绍了如何使用LayoutInflater按钮和editText进行修改的Android Snackbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目现在是如何使Snackbar显示在屏幕底部.小吃店包含editText和三个用于选择类别的按钮. 我使用LayoutInflater,并按照以下代码进行操作,并且可以正常工作.我现在的问题是,当用户单击按钮时如何获得响应? 这是我的代码

My project now is how to make a Snackbar displayed at the bottom of the screen. The Snackbar contain with editText and three button for selection categories. I use LayoutInflater and follow these code for my script, and it's work. My problem now is, how to get response when user click the button ? Here my code

         @Override
         public void onClick(View v) {

            CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);

            final Snackbar snackbar = Snackbar.make(linearLayout, "", 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();

        }

这是我的snacbar.xml布局

and here my snacbar.xml layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:id="@+id/event_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollHorizontally="false"
    android:hint="@string/type_your_message"
    android:inputType="textNoSuggestions"
    android:maxLines="4"
    android:singleLine="false"
    android:textSize="18sp"
    android:paddingLeft="4dp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn_mapel"
    android:onClick="mapel"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/fab_margin_bottom"
    android:layout_marginRight="@dimen/fab_margin_right"
    android:src="@drawable/ic_image_edit" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/fab_margin_bottom"
    android:layout_marginRight="@dimen/fab_margin_right"
    android:src="@drawable/ic_image_edit" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="@dimen/fab_margin_bottom"
    android:layout_marginRight="@dimen/fab_margin_right"
    android:src="@drawable/ic_image_edit" />
</LinearLayout>

推荐答案

此处是实现对小吃店自定义布局的按钮侦听器的来源.

here is the source required to implement button listener to your snackbar custom layout.

@Override
     public void onClick(View v) {

        CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);

        final Snackbar snackbar = Snackbar.make(linearLayout, "", 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

        // button id for snackbar
        Button button_mapel = (Button) snackView.findViewById(R.id.btn_mapel);

        // perform button click listener
        button_mapel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // here perform your button click task
           }
        });

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

    }

这篇关于如何使用LayoutInflater按钮和editText进行修改的Android Snackbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆