自定义viewDialog

1.dialog style:

<style name="MyDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
    </style>

2.自定义继承自Dialog的类:

package com.dy.sso.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.dy.ssosdk.R;


/** * Created by:Pxy * Date: 2016-03-08 * Time: 16:08 */
public class BindPhoneDialog extends Dialog{
    private ImageView ivClose;
    private EditText edtPhone;
    private EditText  edtCode;
    private Button btnConfirm;
    private View.OnClickListener  listener;

    public BindPhoneDialog(Context context, int theme) {
        super(context, theme);
        this.listener= (View.OnClickListener) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);

        ivClose= (ImageView) findViewById(R.id.iv_close);
        edtPhone= (EditText) findViewById(R.id.edt_phone);
        edtCode= (EditText) findViewById(R.id.edt_code);
        btnConfirm= (Button) findViewById(R.id.btn_confirm);
       //添加监听器
        ivClose.setOnClickListener(listener);
        btnConfirm.setOnClickListener(listener);
    }

    public String  getPhone(){
        return edtPhone.getText().toString();
    }

    public String  getCode(){
        return edtCode.getText().toString();
    }

}

3.调用 并使宽度适应屏幕

  dialog=new BindPhoneDialog(this,R.style.MyDialog);
        dialog.show();
        dialog.setCancelable(false);

        Window win = dialog.getWindow();
        win.getDecorView().setPadding(30, 0,30, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

相应点击事件:

 @Override
    public void onClick(View view) {
        int id=view.getId();
        if(id== com.dy.ssosdk.R.id.iv_close){
            dialog.dismiss();
        }else if(id== com.dy.ssosdk.R.id.btn_confirm){
            Log.i("666", dialog.getPhone()+dialog.getCode());
        }
    }

你可能感兴趣的:(dialog)