先贴xml代码
然后是Activity代码
很简单,你会用了吗?
<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();
}
}
很简单,你会用了吗?