提示框在一个完整的app中必不可缺,闲来无事,给大家分享一个简单粗暴易操作的dialog的实现。
首先将提示框中的内容在xml文件中布局出来。
<span style="color:#009900;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:background="#ffffff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示"
android:textColor="#000000"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示的内容出来了了了了了"
android:textColor="#000000"
android:textSize="20dp"
android:paddingTop="10dp"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
>
<TextView
android:id="@+id/tv_no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
android:textColor="#000000"
android:textSize="25dp"
android:gravity="center_horizontal"
/>
<TextView
android:id="@+id/tv_yes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"
android:textColor="#000000"
android:textSize="25dp"
android:gravity="center_horizontal"
/>
</LinearLayout>
</LinearLayout>
</span>
然后自定义一个ActivityDialog继承dialog。其中要注意一定要加(this.requestWindowFeature(Window.FEATURE_NO_TITLE);)这句话,要不然自定义的dialog中会出现标题栏。
<span style="color:#009900;">public class ActivityDialog extends Dialog{
private Context context;
private TextView tv_no,tv_yes;
public ActivityDialog(Context context) {
super(context);
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
super.onCreate(savedInstanceState);
LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.dialog, null);
tv_no=(TextView) view.findViewById(R.id.tv_no);
tv_yes=(TextView) view.findViewById(R.id.tv_yes);
tv_no.setOnClickListener(new View.OnClickListener() {//取消的点击事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();//退出提示框
}
});
tv_yes.setOnClickListener(new View.OnClickListener() {//确定的点击事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//这里写确定后要做的操作
dismiss();//退出提示框
}
});
setContentView(view);
}
}</span>
最后就是在MainActivity中引用了
<span style="color:#009900;">public class MainActivity extends Activity {
private Button bt_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_show=(Button) findViewById(R.id.bt_show);
bt_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ActivityDialog dialog=new ActivityDialog(MainActivity.this);
dialog.show();
}
});
}
}</span>