您的位置:首页 > 移动开发 > Android开发

Android 自定义 DialogFragment 宽度问题

2017-05-11 20:09 465 查看
package com.eno.pages.trade;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.TextView;

import com.eno.base.utils.TCRS;
import com.eno.common.log.Logs;
import com.eno.common.views.CustomDialog;
import com.eno.pages.account.bean.TradeFieldBean;
import com.eno.pages.trade.ordinaryTrade.mvp.presenter.TradePtBuySellPresenter;
import com.eno.pages.trade.ordinaryTrade.mvp.view.I_View_TradePt_Buy_Sell;
import com.eno.pages.trade.tradeConsts.TradeConsts;
import com.eno.system.ENOSystem;
import com.eno.xyzq.page.R;

import java.util.HashMap;
import java.util.Map;

import static com.eno.common.utils.DialogUtils.CommonSingleBtnDialog;
import static com.eno.xyzq.page.R.id.tv_total_num;

/**
* 作者:willkong on 2016/12/16.
* QQ号:547860818
* 作用:普通买卖交易处理
*/

@SuppressLint("ValidFragment")
public class NewOrdinaryTradeDialogFragment extends DialogFragment implements View.OnClickListener {
private TradePtBuySellPresenter bs_p;//买卖p
public static final int BUY = 6;//正常买卖
public static final int BUY_CONFIRM = 183;//退市股风险提示及签约
public static final int BUY_DELIST = 188;
public static final int BUY_ST = 189;//ST股风险提示及签约
public int subFunction = BUY;//默认子功能号为6

private boolean isMarketPrice;
private String buyOrSellFlag;
private Map<String, String> map = new HashMap<>();
public ENOSystem enoSystem;
private Context context;
private TextView tv_trade_title;
private TextView tv_stockName;
private TextView tv_stockCode;
private TextView tv_price;
private TextView tv_num;
private TextView neutral_button;

private TextView positive_button;
private TextView tv_account;//股东账号
private TextView tv_trade_count;//交易数量
private TextView tv_trade_price;//交易价格

public NewOrdinaryTradeDialogFragment(Context context, ENOSystem enoSystem, Map<String, String> map, String buyOrSellFlag,boolean isMarketPrice) {
this.context = context;
this.enoSystem = enoSystem;
this.map = map;
this.buyOrSellFlag = buyOrSellFlag;
bs_p = new TradePtBuySellPresenter(context);
this.isMarketPrice = isMarketPrice;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_MinWidth);//得到全屏显示
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
View view = inflater.inflate(R.layout.fast_trade_dialog_view, container);
initView(view);
getSetData();
return view;
}

@Override
public void onStart() {
super.onStart();
//计算宽高大小,只显示90%
Dialog dialog = getDialog();
if (dialog != null) {
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
dialog.getWindow().setLayout((int) (dm.widthPixels * 0.9), ViewGroup.LayoutParams.WRAP_CONTENT);
}
}

private void initView(View view) {
tv_trade_title = (TextView) view.findViewById(R.id.tv_trade_title);
tv_stockName = (TextView) view.findViewById(R.id.tv_stockName);
tv_stockCode = (TextView) view.findViewById(R.id.tv_stockCode);
tv_price = (TextView) view.findViewById(R.id.tv_price);
tv_num = (TextView) view.findViewById(R.id.tv_num);
neutral_button = (TextView) view.findViewById(R.id.neutral_button);
positive_button = (TextView) view.findViewById(R.id.positive_button);
tv_account = (TextView) view.findViewById(R.id.tv_account);
tv_trade_count = (TextView) view.findViewById(R.id.tv_trade_count);
tv_trade_price = (TextView) view.findViewById(R.id.tv_trade_price);
neutral_button.setOnClickListener(this);
positive_button.setOnClickListener(this);
}

/**
* 设置弹框信息显示
*/
private void getSetData() {
tv_account.setText(map.get("secuid"));//股东账号
tv_stockName.setText(map.get("stockName"));//股票名称
tv_stockCode.setText(map.get("stockCode"));//股票代码
tv_price.setText(map.get("price"));//价格
tv_num.setText(map.get("qty"));//数量
if (buyOrSellFlag.equals(TradeConsts.tradeFlagBuy)) {
tv_trade_title.setText("买入确认");
tv_trade_count.setText("买入数量");
tv_trade_price.setText("买入价格");
positive_button.setText("确定买入");
tv_num.setTextColor(ContextCompat.getColor(context, R.color.dialog_red));
tv_price.setTextColor(ContextCompat.getColor(context, R.color.dialog_red));
positive_button.setTextColor(ContextCompat.getColor(context, R.color.dialog_red));
} else {
tv_trade_title.setText("卖出确认");
tv_trade_count.setText("卖出数量");
tv_trade_price.setText("卖出价格");
positive_button.setText("确定卖出");
tv_num.setTextColor(ContextCompat.getColor(context, R.color.dialog_blue));
tv_price.setTextColor(ContextCompat.getColor(context, R.color.dialog_blue));
positive_button.setTextColor(ContextCompat.getColor(context, R.color.dialog_blue));
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.neutral_button:
getDialog().dismiss();//消失窗口
break;
case R.id.positive_button:
bs_p.sendRequest(new TradePtBuySellPresenterImp());//发送买卖请求
getDialog().dismiss();
break;
}
}

/**
* 交易委托成功处理方法
*
* @param rs
*/
private void tradeSuccess(TCRS[] rs) {
if (buyOrSellFlag.equals(TradeConsts.tradeFlagBuy)) {
CommonSingleBtnDialog(context, "买入交易已提交,合同号为:" + rs[0].getString("ordersno"));
if (listener != null) {
listener.onDialogFragmentClick();
}
} else {
CommonSingleBtnDialog(context, "卖出交易已提交,合同号为:" + rs[0].getString("ordersno"));
if (listener != null) {
listener.onDialogFragmentClick();
}
}
}

/**
* 普通买卖接口
*/
class TradePtBuySellPresenterImp implements I_View_TradePt_Buy_Sell {

/**
* 需要传入参数:买卖方向、股票代码、股票名称、价格、数量、市场、委托属性、股东号
*
* @return
*/
@Override
public TradeFieldBean getParams() {
TradeFieldBean bean = new TradeFieldBean();
if (buyOrSellFlag.equals(TradeConsts.tradeFlagBuy)) {//买卖方向
bean.setBsFlag("B");
} else {
bean.setBsFlag("S");
}
if (subFunction == BUY || subFunction == BUY_CONFIRM || subFunction == BUY_DELIST || subFunction == BUY_ST) {
bean.setTc_sfuncno(subFunction);
bean.setCode(map.get("stockCode"));//股票代码
bean.setStockName(map.get("stockName"));//股票名称
bean.setQty(map.get("qty"));//数量
bean.setMarket(map.get("market"));//市场
if (isMarketPrice){
bean.setPrice("");//价格
bean.setEntrust_prop(map.get("entrust_prop"));//委托属性--0-买卖 3-申购 (股转 d) (市价委托 上海:R-最优五档即时成交剩余转限价,U-最优五档即时成交剩余撤销  深圳:Q-对手方最优价格,S-本方最优价格,T-即时成交剩余撤销,U-最优五档即时成交剩余撤销,V-全额成交或撤单)
}else {
bean.setPrice(map.get("price"));//价格
bean.setEntrust_prop("0");
}
bean.setSecuid(map.get("secuid"));//股东号
}
return bean;
}

@Override
public void onResponse(TCRS[] tcrs) {
processData(tcrs);//数据处理
}

@Override
public void errorMessage(int errorId, String error) {
CommonSingleBtnDialog(context, error);
}
}

/**
* 数据处理方法
*
* @param rs
*/
private void processData(TCRS[] rs) {
//Field:   0_[9](ordersno)合同编号  1_[9](error_no)错误号  2_[9](error_info)错误信息
if (subFunction == BUY) {//第一次,判断ST股
String fieldname = rs[0].getFieldName(0);
if (fieldname.equals("delist_note")) {// ST风险警示
showApply(0);
} else if (fieldname.equals("delist_flag")) { // 退市整理
String delistDate = rs[0].toString("delist_date");
showWarning(delistDate);
} else { // 成功-不是ST股
tradeSuccess(rs);
}
} else if (subFunction == BUY_CONFIRM) {
String fieldname = rs[0].getFieldName(0);
if (fieldname.equals("delist_note")) { // 退市整理
if (map.get("market").equals("0")) {
showApply(2);
} else if (map.get("market").equals("1")) {
showApply(1);
}
} else { // 成功
tradeSuccess(rs);
}
} else if (subFunction == BUY_DELIST || subFunction == BUY_ST) {
tradeSuccess(rs);
}
}

/**
* ST风险警示弹框 //ST股风险提示及签约
*
* @param type
*/
private void showApply(final int type) {
String plate = "";
if (type == 0) {
plate = "上海A股风险警示股票";
} else if (type == 1) {
plate = "上海A股退市整理股票";
} else if (type == 2) {
plate = "深圳A股退市整理股票";
}
Logs.e("尊敬的客户,您尚无" + plate + "交易权限,请先申请该权限。");
View view = View.inflate(context, R.layout.protocol_sign, null);
final AlertDialog dialog = new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("网上交易用户协议")
.setView(view)
.setPositiveButton("申请", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (type == 0) {
subFunction = BUY_ST;
} else if (type == 1 || type == 2) {
subFunction = BUY_DELIST;
}
bs_p.sendRequest(new TradePtBuySellPresenterImp());
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);

WebView content = (WebView) view.findViewById(R.id.protocol_content);
if (type == 0)
content.loadUrl("http://download.xyzq.com.cn/stsb/shfx.html");
else if (type == 1)
content.loadUrl("http://download.xyzq.com.cn/stsb/shts.html");
else if (type == 2)
content.loadUrl("http://download.xyzq.com.cn/stsb/szzz.html");
final CheckBox check = (CheckBox) view.findViewById(R.id.protocol_check);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (check.isChecked())
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
else
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
}
});
}

/**
* 退市整理股风险提示弹框 //退市股风险提示及签约
*
* @param delistDate
*/
private void showWarning(String delistDate) {
String message = map.get("stockName") + "即将终止上市,终止日期:" + delistDate + ",请注意退市风险,谨慎投资。";
CustomDialog.Builder dialog = new CustomDialog.Builder(context);
dialog.setTitle("退市提醒");
dialog.setMessage(message);
dialog.setPositiveButton("继续委托", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
subFunction = BUY_CONFIRM;
bs_p.sendRequest(new TradePtBuySellPresenterImp());
}
});
dialog.setNegativeButton("取消委托", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
dialog.show();
}

/**
* 定义接口,当请求成功后回调--重新请求最大可买可卖刷新页面
*/
public interface OnDialogFragmentClickListener {
void onDialogFragmentClick();
}

public OnDialogFragmentClickListener listener;

public void setOnDialogFragmentClickListener(OnDialogFragmentClickListener listener) {
this.listener = listener;
}
}


自定义宽度

方式一、

使对话框充满屏幕宽度

这种方式是使对话框充满屏幕的宽度,通过设置 Diaglog 样式实现;在onCreate方法中修改如下:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light_Dialog_MinWidth);//得到全屏显示
}


方式二、

占用屏幕宽度一定比例

这种方式是自己计算屏幕宽度并设置到对话框,重写onStart方法,实现如下

@Override
public void onStart() {
super.onStart();
//计算宽高大小,只显示90%
Dialog dialog = getDialog();
if (dialog != null) {
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
dialog.getWindow().setLayout((int) (dm.widthPixels * 0.9), ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: