Android AlertDialog的使用

先贴xml代码
<LinearLayout
    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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="davidwill.crazeandroid.DialogActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textviewNotification"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnShowDialog"
        android:text="@string/showAlertDialog"/>
</LinearLayout>


然后是Activity代码
public class DialogActivity extends Activity implements View.OnClickListener
{

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

        btnShowAiertDialog = (Button) findViewById(R.id.btnShowDialog);
        textviewShowNotification = (TextView) findViewById(R.id.textviewNotification);
        btnShowAiertDialog.setOnClickListener(this);

        alertDialog = new AlertDialog.Builder(this);
        alertDialog.setIcon(R.drawable.a)
                .setTitle(R.string.app_name)
                .setMessage(R.string.i_am_dialog)
                .setPositiveButton(getString(R.string.sure), new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        textviewShowNotification.setText(R.string.userClickedSure);
                    }
                })
                .setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        textviewShowNotification.setText(R.string.userClickedCancel);
                    }
                });
   }

    private Button btnShowAiertDialog;
    private AlertDialog.Builder alertDialog;
    private TextView textviewShowNotification;

    @Override
    public void onClick(View v)
    {
        alertDialog.create().show();
    }
}


很简单,你会用了吗?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值