效果图:
代码部分
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击弹窗"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="207dp"
android:layout_marginEnd="207dp"
android:layout_marginTop="165dp" />
</RelativeLayout>
MainActivity.java:
package com.example.mydialog;
import android.app.Activity;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import dialog.MessageFragment;
public class MainActivity extends ActionBarActivity {
//提示框fragment
private MessageFragment mMessageFragment;
//bundle用来传递提示框的信息
private Bundle bundle;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showdialog();
}
});
}
private void showdialog() {
if (mMessageFragment == null) {
mMessageFragment = new MessageFragment();
}
if (bundle == null) {
bundle = new Bundle();
}
bundle.putString("content", "是否确认收件");
bundle.putString("title", "标题");
mMessageFragment.setOnResultListener(new OnItemMsgResultListener());
mMessageFragment.setArguments(bundle);
// 显示提示窗口
mMessageFragment.show(getFragmentManager(), "");
}
class OnItemMsgResultListener implements MessageFragment.OnMsgResultListener {
public OnItemMsgResultListener() {
}
@Override
public void onResultFun(int resultCode) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(MainActivity.this,"确认",Toast.LENGTH_SHORT).show();
// getAliasData();
}else{
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
}
}
}
提示框的布局文件:
message_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/tv_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@mipmap/confirm_head">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="46.0dp"
android:textSize="18sp"
android:textColor="#fff"
android:text="提示"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlyt_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_head"
android:background="@mipmap/message_content">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="xxxx"
android:textColor="#1a1a1a"
android:textSize="15sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rlyt_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_msg_submit"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btn_submit"
android:background="@drawable/button_msg_cancel"/>
<View
android:id="@+id/approve_ver_line"
android:layout_width="0.5dp"
android:layout_height="41.0dp"
android:layout_toRightOf="@+id/btn_submit"
android:background="@color/divider_gray" />
</RelativeLayout>
<View
android:id="@+id/message_hor_line"
android:layout_width="296.0dp"
android:layout_height="0.5dp"
android:layout_marginLeft="4.5dp"
android:layout_below="@+id/rlyt_content"
android:background="@color/divider_gray" />
</RelativeLayout>
drawable下的xml文件:
button_msg_cancel.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/btn_msg_cancel_press" />
<item android:state_enabled="false" android:drawable="@mipmap/btn_msg_cancel_normal" />
<item android:drawable="@mipmap/btn_msg_cancel_normal" />
</selector>
button_msg_submit.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/btn_msg_submit_press" />
<item android:state_enabled="false" android:drawable="@mipmap/btn_msg_submit_normal" />
<item android:drawable="@mipmap/btn_msg_submit_normal" />
</selector>
其他的png图片资源在链接:http://download.csdn.net/detail/u010778159/8923083
最后,
MessageFragment.java:
package dialog;
import android.app.Activity;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import com.example.mydialog.R;
public class MessageFragment extends DialogFragment {
public static final String TAG = "MessageDialog";
private OnMsgResultListener mOnMsgResultListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//message_dialog是提示框的布局
View show_view = inflater.inflate(R.layout.message_dialog, null);
TextView mTxtContent = (TextView) show_view.findViewById(R.id.tv_content);
TextView mTxtTitle = (TextView) show_view.findViewById(R.id.tv_title);
Button btnSubmit = (Button) show_view.findViewById(R.id.btn_submit);
Button btnCancel = (Button) show_view.findViewById(R.id.btn_cancel);
Bundle bundle = this.getArguments();
if(bundle != null){
String title = bundle.getString("title");
if(title!=null){
mTxtTitle.setText(bundle.getString("title"));
}
mTxtContent.setText(bundle.getString("content"));
}
btnSubmit.setOnClickListener(btnClickListener);
btnCancel.setOnClickListener(btnClickListener);
setCancelable(false);
return show_view;
}
public void setOnResultListener(OnMsgResultListener listener) {
mOnMsgResultListener = listener;
}
View.OnClickListener btnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_submit:
mOnMsgResultListener.onResultFun(Activity.RESULT_OK);
dismiss();
break;
case R.id.btn_cancel:
mOnMsgResultListener.onResultFun(Activity.RESULT_CANCELED);
dismiss();
break;
default:
break;
}
}
};
public interface OnMsgResultListener {
void onResultFun(int resultCode);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 13:38:26