OF PROGRAMMING MOBILE APPLICATIONS FOR ANDROID LAB NOTES

advertisement
Department of Information Networks
University of Babylon
LAB NOTES OF PROGRAMMING
MOBILE APPLICATIONS FOR
ANDROID
By
Dr. Samaher Hussein Ali
Bashar Hamid
Wesam Hussain
College of Information Technology, University of Babylon, Iraq
Samaher@inet.uobabylon.edu.iq
Notes of Lab 5
29 November 2014
‫‪Alert Dialog‬‬
‫‪ -1‬نقوم بادراج (‪ )Button‬من االدوات الموجود ضمن جزء ال ‪Form‬‬
‫‪ Widgets‬ثم نقوم بتغيير خاصيتي ال ‪ text‬وال‪ID‬‬
‫‪Notes of Lab 5‬‬
‫‪29 November 2014‬‬
Alert Dialog
‫ و نقوم بتعريف متغير من نوع‬MainActivity.java ‫ ننتقل الى ملف‬-2
‫ ثم نقوم بأخذ‬Button‫ ب ال‬OnClickListener ‫) و ربط الكالس‬Button(
.‫ لكي نتعامل معها‬AlertDialog.Builder ‫نسخة من الكالس‬
Class
Object
Notes of Lab 5
29 November 2014
Alert Dialog
‫ يمكن الوصول لكل الدوال و الخصائص الموجودة‬dialog ‫ بإستخدام النسخة‬-3
.‫ضمن الكالس‬
-setTitle(String title)
This method set the title to be appear in the dialog.
-setIcon(Drawable icon)
This method set the icon of the alert dialog box.
-setMessage(String message)
This method sets the message to be displayed in the dialog.
Notes of Lab 5
29 November 2014
Alert Dialog
-setPositiveButton(String text, DialogInterface.OnClickListener
listener): set the positive (Yes) button using the object of the
AlertDialogBuilder class.
-setNegativeButton(String text, DialogInterface.OnClickListener
listener): set the negative (No) button using the object of the
AlertDialogBuilder class.
29 November 2014
Notes of Lab 5
Alert Dialog
-setMultiChoiceItems(String[] items, boolean[] checkedItems,
DialogInterface.OnMultiChoiceClickListener listener):This method sets
list of items to be displayed in the dialog as the content. The selected
option will be notified by the listener.
Notes of Lab 5
29 November 2014
Alert Dialog
- You will create an alert dialog by calling the create() method of the
builder class.
- show():show the alert dialog on the screen.
Notes of Lab 5
29 November 2014
Alert Dialog
The Code :
public class MainActivity extends Activity {
String[] items = {"Google","Youtube","Facebook","Yahoo","Gmail","WiKi"};
boolean[] items_selected = new boolean[items.length];
String selectedItem = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.btn1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Choose your class");
dialog.setIcon(R.drawable.ic_launcher);
29 November 2014
Notes of Lab 5
Alert Dialog
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedItem="";
for (int i = 0; i < items.length; i++) {
if (items_selected[i] ==true) {
selectedItem=selectedItem+items[i]+" ";
items_selected[i]=false;
}
}
Toast.makeText(getApplicationContext(), "Selected: " + selectedItem, Toast.LENGTH_SHORT).show();
}
});
dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Canceled: " + selectedItem, Toast.LENGTH_SHORT).show();
}
});
29 November 2014
Notes of Lab 5
Alert Dialog
dialog.setMultiChoiceItems(items, items_selected, new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(),items[which] + (isChecked ? " checked!" : "
unchecked!"),Toast.LENGTH_SHORT).show();
}
});
dialog.create();
dialog.show();
}
});
}
29 November 2014
Notes of Lab 5
Download