Snackbar的使用方法和Toast很相似
第一个参数需要传入一个View,可以是界面当中的任意一个View控件,Snackbar会自动根据这个控件找到最外层的布局来显示
第二个参数就是我们需要显示的内容,注意这里的内容最多显示两行哦,超出两行后的内容会变成“…”
第三个参数为Snackbar显示的时长,有三种模式供选择
- LENGTH_SHORT:短时间显示
- LENGTH_LONG:长时间显示
- LENGTH_INDEFINITE:一直显示,只有当用户触发Action点击事件或手动删除时才会消失
Snackbar可以通过setAction方法设置一个点击事件,和用户进行交互
先使用Toast的即时显示消息:
public class BrainTeaser2 extends AppCompatActivity implements AdapterView.OnItemClickListener{
String[] queArr={"什么门永远关不上?","什么东西没人爱吃?",
"什么瓜不能吃?","什么布不能切?",
"什么鼠最爱干净?","偷什么不犯法?"};
String[] aueArr={"球门","亏",
"傻瓜","瀑布",
"环保局","偷笑"};
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brain_teaser2);
ArrayAdapter<String> adapter=new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
queArr);
ListView lv=(ListView)findViewById(R.id.lv2);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
toast=Toast.makeText(this,"",Toast.LENGTH_LONG);
}
public void onItemClick(AdapterView<?> parent , View view,
int poistion, long id){
toast.setText("答案:"+aueArr[poistion]);
//设置Toast的位置
toast.setGravity(Gravity.TOP | Gravity.RIGHT,0,50);
toast.show();
// Toast.makeText(this,"答案:"+aueArr[poistion],
// Toast.LENGTH_LONG).show();
}
}
UI界面设计:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BrainTeaser_Snackbar">
<ListView
android:id="@+id/lv3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
2使用Snackbar即时显示消息的小程序
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener {
// 创建问题数组
String[] queArr = {"什么门永远关不上","什么东西没人爱吃?",
"什么瓜不能吃?","什么布切不断?",
"什么鼠最爱干净?","偷什么不犯法?"};
// 创建答案数组
String[] ansArr = { "球门", "亏",
"傻瓜","瀑布",
" 环保署","偷笑" };
//Toast tos; // 声明 Toast 对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建供 ListView 使用的 ArrayAdapter 对象
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1, // 使用内置的布局资源
queArr); // 以 queArr 数组当数据源
ListView lv = (ListView)findViewById(R.id.lv); //获取 ListView
lv.setAdapter(adapter); //设置 ListView 使用的 Adapter
lv.setOnItemClickListener(this); //设置 ListView 项目被单击时的事件监听器
//tos = tos.makeText(this, "", Toast.LENGTH_SHORT); //创建 Toast 对象
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//tos.setText("答案︰"+ansArr[position]); // 变更 Toast 对象的文字内容
//tos.show(); // 立即重新显示
Snackbar.make(findViewById(R.id.root),
"答案︰"+ansArr[position],
Snackbar.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/root">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>