用Activity的dialog主题实现自定义dialog

要实现这样的效果


先说思路

点击一个按钮弹出一个这样的dialog,其实为点击按钮跳转一个Activity,因为要取得填写的数据,返回到上个界面,用startActivityForResult();

dialog的界面样式就是activity的xml中自定义,非常容易简单,上代码


首先这个dialogActivity得样式

<activity
    android:name=".activity.DialogActivity"
    android:theme="@style/Theme.AppCompat.Dialog" />
它的布局 ,自定义的圆角矩形就不在此写了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="190dp"
    android:background="@drawable/rect_white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="15dp"
        android:gravity="center"
        android:text="报价"
        android:textColor="@color/list"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/rectangle_shape_gray1"
        android:hint="请输入您的报价"
        android:inputType="number"
        android:textColor="@color/list"
        android:textColorHint="@color/text_tab" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:gravity="center">

        <TextView
            android:id="@+id/tv_cancle"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/rectangle_shape_button"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/white"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_ok"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:background="@drawable/rectangle_shape_button1"
            android:gravity="center"
            android:text="完成"
            android:textColor="@color/white"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>
java代码

public class DialogActivity extends Activity {

    private EditText et_content;
    private TextView tv_ok;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_custom_alert);
        et_content = (EditText) findViewById(R.id.et_content);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_content.getText().toString() != null && et_content.getText().toString().length() > 0) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("price", et_content.getText().toString());
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    ToastUtils.showToast(DialogActivity.this, "报价内容不能为空");
                }
            }
        });
        findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
然后再之前一个界面的onActivityResult方法中获得输入的内容

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_PRICE) {
        if (resultCode == RESULT_OK) {
            String price = data.getStringExtra("price");
            addMissionUserTaking(price);
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值