вторник, 28 сентября 2010 г.

Java: Android: Алерты

AlertDialog:



show() - отобразить алерт;
setMessage() - устанавливает текст алерта;
setTitle() - устанавливает тайтл алерта;

setIcon() - устанавливает иконку алерта;
setPositiveButton(),setNeutralButton(),setNegativeButton() - устанавливаются кнопки алерта;

Пример:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/alert"
android:text="Raise an alert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/toast"
android:text="Make a toast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Java:

public class MessageDemo extends Activity implements View.OnClickListener {
Button alert;
Button toast;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
alert=(Button)findViewById(R.id.alert);
alert.setOnClickListener(this);
toast=(Button)findViewById(R.id.toast);
toast.setOnClickListener(this);
}
public void onClick(View view) {
if (view==alert) {
new AlertDialog.Builder(this)
.setTitle("MessageDemo")
.setMessage("eek!")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
// do nothing – it will close on its own
}
})
.show();
}
else {
Toast
.makeText(this, "<clink, clink>", Toast.LENGTH_SHORT)
.show();
}
}
}

Комментариев нет:

Отправить комментарий

Примечание. Отправлять комментарии могут только участники этого блога.