new -> XML -> Layout XML File , 如下圖1-2 : ui當(dāng)中的重要的控件..."/>
您好,登錄后才能下訂單哦!
一 : UI設(shè)計(jì)
1 , Dialog(對(duì)話框)
1-1 : 新建 dialog_bay.xml , 方法 : 右擊“l(fā)ayout” -> new -> XML -> Layout XML File , 如下圖
1-2 : ui當(dāng)中的重要的控件
①,(RadioGroup)rg_type : 單選按鈕組
①-1 : (RadioButton)rb_zfb : 支付寶
①-2 : (RadioButton)rb_wx : 微信
②, (Button)btn_ok : 確定按鈕
1-3 : 界面布局如下:
2,activity_main.xml(主界面UI)設(shè)計(jì)
2-1 : ui當(dāng)中的重要的控件
① , (Button) btn_pay : 確定支付
2-2 : 界面如下:
2-3 : 注意 , 為簡化起見,沒有放商品圖片 。這里重點(diǎn)講解對(duì)話框
二 :代碼設(shè)計(jì)
①,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) {
//創(chuàng)建View
this._contentView = inflater.inflate( R.layout.dialog_pay , container);
//點(diǎn)擊按鈕關(guān)閉對(duì)話框
Button $button = (Button)this._contentView.findViewById(R.id.btn_ok);
$button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//關(guān)閉
DialogPay.this.dismiss();
//返回?cái)?shù)據(jù)
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 , "對(duì)話框選擇的支付方式: " + $option );
if(null != this._listener){
this._listener.onDialogPayReturn( $option );
}
}
@Override
public void onStart() {
super.onStart();
//當(dāng)對(duì)話框顯示時(shí) , 調(diào)整對(duì)話框的窗口位置
Window $win = getDialog().getWindow();
if( null != $win ){
$win.setBackgroundDrawable( new ColorDrawable(Color.WHITE));
//設(shè)置對(duì)話框的窗口顯示
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 包
定義了一個(gè)類似與一個(gè)監(jiān)聽器
② ,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) {
//處理對(duì)話框返回來的數(shù)據(jù)
Toast.makeText(MainActivity.this, "對(duì)話框返回的數(shù)據(jù):" + $option, Toast.LENGTH_SHORT).show();
}
});
$dl.show(getFragmentManager(),"DialogPay");
}
});
}
}
注意 , 為對(duì)話框注冊(cè)監(jiān)聽器
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。