如何修改 Android 包含 LayoutInflater 按钮和 editText 的 Snackbar
How to make modification Android Snackbar containing with LayoutInflater button and editText
我现在的项目是如何在屏幕底部显示一个Snackbar。 Snackbar 包含 editText 和三个用于选择类别的按钮。
我使用 LayoutInflater 并在我的脚本中遵循这些 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 布局
<?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>
遵循此文档
public class MyUndoListener implements View.OnClickListener{
&Override
public void onClick(View v) {
// Code to undo the user's last action
}
}
现在为 setAction()
Snackbar mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),
R.string.email_archived, Snackbar.LENGTH_SHORT);
mySnackbar.setAction(R.string.undo_string, new MyUndoListener());
mySnackbar.show();
试试这个,
Button btn_feed_back = (Button)snackView.findViewById(R.id.feedback);
btn_feed_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
您必须获取 Button
的 ID。并且必须设置 clickListener
.
你只需要用这个替换你的代码。
我已经把listener放在了btn_mapel
,你可以根据自己的需要放其他按钮。
试试这个。
@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 mapelBt = (Button)snackView.findViewById(R.id.btn_mapel);
mapelBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get your button click callback here
}
});
layout.addView(snackView, 0);
snackbar.show();
}
这里是为您的 snackbar 自定义布局实现按钮侦听器所需的源代码。
@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();
}
我现在的项目是如何在屏幕底部显示一个Snackbar。 Snackbar 包含 editText 和三个用于选择类别的按钮。 我使用 LayoutInflater 并在我的脚本中遵循这些 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 布局
<?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>
遵循此文档
public class MyUndoListener implements View.OnClickListener{
&Override
public void onClick(View v) {
// Code to undo the user's last action
}
}
现在为 setAction()
Snackbar mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),
R.string.email_archived, Snackbar.LENGTH_SHORT);
mySnackbar.setAction(R.string.undo_string, new MyUndoListener());
mySnackbar.show();
试试这个,
Button btn_feed_back = (Button)snackView.findViewById(R.id.feedback);
btn_feed_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
您必须获取 Button
的 ID。并且必须设置 clickListener
.
你只需要用这个替换你的代码。
我已经把listener放在了btn_mapel
,你可以根据自己的需要放其他按钮。
试试这个。
@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 mapelBt = (Button)snackView.findViewById(R.id.btn_mapel);
mapelBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get your button click callback here
}
});
layout.addView(snackView, 0);
snackbar.show();
}
这里是为您的 snackbar 自定义布局实现按钮侦听器所需的源代码。
@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();
}