【实例简介】安卓两种底部弹出窗dialog实现方式,一种是列表弹窗实现,2是灵活底部弹窗,传入布局以及控件id
【实例截图】
【核心代码】
package com.kx.kxbottomdialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mContext = this;
findViewById(R.id.bt1).setOnClickListener(this);
findViewById(R.id.bt2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
bt1ListView();
break;
case R.id.bt2://退出登录
Bt2Loginout();
break;
}
}
/**
* 底部列表弹窗
*/
private void bt1ListView() {
List names = new ArrayList<>();
names.add("随堂测验");
names.add("单元考试");
names.add("期中(末)考试");
showDialog(new BottomCirTraDialog.SelectDialogListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
switch (position) {
case 0: //随堂测验
showToast("随堂测验");
break;
case 1://单元考试
showToast("单元考试");
break;
case 2://期中(末)考试
showToast("期中(末)考试");
break;
default:
break;
}
}
}, names);
}
private BottomCirTraDialog showDialog(BottomCirTraDialog.SelectDialogListener listener, List names) {
BottomCirTraDialog dialog = new BottomCirTraDialog(this, R.style.transparentFrameWindowStyle, listener, names);
if (!this.isFinishing()) {
dialog.show();
}
return dialog;
}
/**
* 退出登录
*/
private void Bt2Loginout() {
BottomDialog dialog = new BottomDialog(mContext, R.layout.dialog_login_out,
new int[]{R.id.tv_choice_out, R.id.tv_cancel});
dialog.show();
dialog.setOnBottomItemClickListener(new BottomDialog.OnBottomItemClickListener() {
@Override
public void onBottomItemClick(BottomDialog dialog, View view) {
switch (view.getId()) {
case R.id.tv_choice_out: //退出登录
//服务器登出 mCenterPI.logout();
dialog.cancel();
break;
case R.id.tv_cancel: //取消
dialog.cancel();
break;
}
}
});
}
/**
* Toast
*
* @param s
*/
public void showToast(String s) {
ToastUtil.toast(this, s);
}
}