要实现这样的效果
先说思路
点击一个按钮弹出一个这样的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); } } }