2,activity_main.xml(主界面UI)设计
2-1 : ui当中的重要的控件
① , (Button) btn_pay : 确定支付
2-2 : 界面如下:
2-3 : 注意 , 为简化起见,没有放商品图片 。这里重点讲解对话框
二 :代码设计
①,DialogPay.java
package com.example.kayer.dialogdemo;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioGroup;
public final class DialogPay extends DialogFragment {
private final String TAG = "测试Pay";
private View _contentView = null;
private OnDialogPayListener _listener = null;
public interface OnDialogPayListener{
void onDialogPayReturn( String $option );
}
public void setOnDialogPayListener( OnDialogPayListener $listener ){
this._listener = $listener;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//创建View
this._contentView = inflater.inflate( R.layout.dialog_pay , container);
//点击按钮关闭对话框
Button $button = (Button)this._contentView.findViewById(R.id.btn_ok);
$button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//关闭
DialogPay.this.dismiss();
//返回数据
DialogPay.this.returnInfos();
}
});
return this._contentView;
}
public void returnInfos(){
RadioGroup $rg = (RadioGroup)this._contentView.findViewById(R.id.rg_type);
int $rb_id = $rg.getCheckedRadioButtonId();
String $option = $rb_id == R.id.rb_zfb ? "支付宝" : "微信";
Log.d(this.TAG , "对话框选择的支付方式: " + $option );
if(null != this._listener){
this._listener.onDialogPayReturn( $option );
}
}
@Override
public void onStart() {
super.onStart();
//当对话框显示时 , 调整对话框的窗口位置
Window $win = getDialog().getWindow();
if( null != $win ){
$win.setBackgroundDrawable( new ColorDrawable(Color.WHITE));
//设置对话框的窗口显示
WindowManager.LayoutParams $lp = $win.getAttributes();
$lp.dimAmount = 0.3f;//背景灰度
$lp.gravity = Gravity.BOTTOM;//靠下显示
$lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
$lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
$win.setAttributes($lp);
}
}
}
注意 : DialogFragment 是出自 android.app.Dialogfragment 包
定义了一个类似与一个监听器
② ,MainActivity.java
package com.example.kayer.dialogdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btn_pay = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.init();
}
private void init(){
this.btn_pay = (Button)findViewById(R.id.btn_pay);
this.btn_pay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogPay $dl = new DialogPay();
$dl.setOnDialogPayListener(new DialogPay.OnDialogPayListener() {
@Override
public void onDialogPayReturn(String $option) {
//处理对话框返回来的数据
Toast.makeText(MainActivity.this, "对话框返回的数据:" + $option, Toast.LENGTH_SHORT).show();
}
});
$dl.show(getFragmentManager(),"DialogPay");
}
});
}
}
注意 , 为对话框注册监听器
原文地址:http://blog.51cto.com/aonaufly/2129107