Hộp thoại là một cửa sổ nhỏ nhắc người dùng ra quyết định hoặc nhập thêm thông tin.
Đôi khi trong ứng dụng lập trình Android của bạn, nếu bạn muốn hỏi người dùng về việc đưa ra quyết định giữa có hoặc không để phản hồi bất kỳ hành động cụ thể nào được thực hiện bởi người dùng, bằng cách duy trì trong cùng một hoạt động và không thay đổi màn hình, bạn có thể sử dụng Hộp thoại cảnh báo.
Để tạo một hộp thoại cảnh báo, bạn cần tạo một đối tượng của AlertDialogBuilder, một lớp bên trong của AlertDialog. Cú pháp của nó được đưa ra dưới đây.
Sau khi tạo và thiết lập trình xây dựng hộp thoại, bạn sẽ tạo một hộp thoại cảnh báo bằng cách gọi phương thức create () của lớp trình tạo. Cú pháp của nó là
Để thử nghiệm với ví dụ này, bạn cần chạy nó trên trình giả lập hoặc thiết bị thực tế.
Đây là mã sửa đổi của src / MainActivity.java
Trong mã dưới đây abc chỉ ra logo của tutspoint.com
Để chạy ứng dụng từ studio Android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ.
Trước khi bắt đầu ứng dụng của bạn,] Studio Android sẽ hiển thị cửa sổ sau để chọn tùy chọn nơi bạn muốn chạy Khóa học lập trình Android của mình.
Chọn một tùy chọn của bạn và sau đó nhấp vào nó. Giả sử, nếu bạn đã nhấp vào nút có, thì kết quả sẽ như sau
Nếu bạn nhấp vào không có nút nào, nó sẽ gọi kết thúc () và nó sẽ đóng ứng dụng của bạn.
Đôi khi trong ứng dụng lập trình Android của bạn, nếu bạn muốn hỏi người dùng về việc đưa ra quyết định giữa có hoặc không để phản hồi bất kỳ hành động cụ thể nào được thực hiện bởi người dùng, bằng cách duy trì trong cùng một hoạt động và không thay đổi màn hình, bạn có thể sử dụng Hộp thoại cảnh báo.
Để tạo một hộp thoại cảnh báo, bạn cần tạo một đối tượng của AlertDialogBuilder, một lớp bên trong của AlertDialog. Cú pháp của nó được đưa ra dưới đây.
![]() |
| Khóa học lập trình Android |
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);Bây giờ bạn phải đặt nút dương (có) hoặc âm (không) bằng cách sử dụng đối tượng của lớp AlertDialogBuilder. Cú pháp của nó là
alertDialogBuilder.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) alertDialogBuilder.setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)Ngoài ra, Khóa học lập trình Android có thể sử dụng các chức năng khác do lớp xây dựng cung cấp để tùy chỉnh hộp thoại cảnh báo. Chúng được liệt kê dưới đây.
| Không. | Kiểu phương pháp & mô tả |
|---|---|
| 1 | setIcon (Biểu tượng có thể vẽ) Phương pháp này đặt biểu tượng của hộp thoại cảnh báo. |
| 2 | set Hủy được (có thể hủy boolean) Phương thức này đặt thuộc tính mà hộp thoại có thể bị hủy hoặc không |
| 3 | setMessage (tin nhắn CharSequence) Phương pháp này đặt thông báo sẽ được hiển thị trong hộp thoại cảnh báo |
| 4 | setMultiChoiceItems (CharSequence [] mục, boolean [] đã đánh dấu, DialogInterface.OnMultiChoiceClickListener lắng nghe) Phương pháp này đặt danh sách các mục sẽ được hiển thị trong hộp thoại dưới dạng nội dung. Tùy chọn đã chọn sẽ được người nghe thông báo |
| 5 | setOn HủyListener (DialogInterface.On HủyListener on HủyListener) Phương thức này Đặt cuộc gọi lại sẽ được gọi nếu hộp thoại bị hủy. |
| 6 | setTitle (tiêu đề CharSequence) Phương pháp này đặt tiêu đề sẽ xuất hiện trong hộp thoại |
AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show();Điều này sẽ tạo ra hộp thoại cảnh báo và sẽ hiển thị nó trên màn hình.
Đoạn thoại
Trước khi nhập vào một ví dụ, chúng ta cần biết đoạn thoại. Đoạn đoạn là một đoạn có thể hiển thị đoạn trong hộp thoạipublic class DialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { toast.makeText(this,"enter a text here",Toast.LENTH_SHORT).show(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { finish(); }); // Create the AlertDialog object and return it return builder.create(); } } }
Danh sách hộp thoại
Nó được sử dụng để hiển thị danh sách các mục trong hộp thoại. Giả sử, người dùng cần chọn một danh sách các mục hoặc nếu không cần phải nhấp vào một mục từ nhiều danh sách các mục. Trong tình huống này, chúng ta có thể sử dụng hộp thoại danh sách.public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(Pick a Color) .setItems(R.array.colors_array, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // The 'which' argument contains the index position // of the selected item } }); return builder.create(); }
Hộp thoại danh sách lựa chọn đơn
Nó đã được sử dụng để thêm danh sách lựa chọn duy nhất vào hộp thoại. Chúng tôi có thể kiểm tra hoặc bỏ chọn theo lựa chọn của người dùng.public Dialog onCreateDialog(Bundle savedInstanceState) { mSelectedItems = new ArrayList(); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("This is list choice dialog box"); .setMultiChoiceItems(R.array.toppings, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { // If the user checked the item, add it to the selected items mSelectedItems.add(which); } else if (mSelectedItems.contains(which)) { // Else, if the item is already in the array, remove it mSelectedItems.remove(Integer.valueOf(which)); } } }) // Set the action buttons .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // User clicked OK, so save the mSelectedItems results somewhere // or return them to the component that opened the dialog ... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { ... } }); return builder.create(); }Ví dụ sau đây cho thấy việc sử dụng AlertDialog trong Khóa học lập trình Android.
Để thử nghiệm với ví dụ này, bạn cần chạy nó trên trình giả lập hoặc thiết bị thực tế.
| Các bước | Sự miêu tả |
|---|---|
| 1 | Bạn sẽ sử dụng studio Android để tạo một ứng dụng Android và đặt tên là Ứng dụng của tôi theo gói com.example.sairamkrishna.myapplication. |
| 2 | Sửa đổi tệp src / MainActivity.java để thêm mã hộp thoại cảnh báo để khởi chạy hộp thoại. |
| 3 | Sửa đổi bố cục tệp XML res / layout / Activity_main.xml thêm bất kỳ thành phần GUI nào nếu cần. |
| 4 | Không cần thay đổi hằng chuỗi mặc định. Studio Android chăm sóc các chuỗi mặc định tại các giá trị / chuỗi.xml |
| 5 | Chạy ứng dụng và chọn một thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả. |
package com.example.sairamkrishna.myapplication; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void open(View view){ AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("Are you sure, You wanted to make decision"); alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show(); } }); alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() { Override public void onClick(DialogInterface dialog, int which) { finish(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } }Đây là mã được sửa đổi của res / layout / Activity_main.xml
Trong mã dưới đây abc chỉ ra logo của tutspoint.com
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Alert Dialog" android:id="@+id/textView" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorialspoint" android:id="@+id/textView2" android:textColor="#ff3eff0f" android:textSize="35dp" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView2" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Alert dialog" android:id="@+id/button" android:layout_below="@+id/imageView" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" android:layout_marginTop="42dp" android:onClick="open" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" /> </RelativeLayout>Đây là của String.xml
<resources> <string name="app_name">My Application</string> </resources>Đây là mã mặc định của AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapplication.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Khóa học lập trình Android Mobile thực tế của mình với máy tính.
Để chạy ứng dụng từ studio Android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ.
Trước khi bắt đầu ứng dụng của bạn,] Studio Android sẽ hiển thị cửa sổ sau để chọn tùy chọn nơi bạn muốn chạy Khóa học lập trình Android của mình.
Chọn một tùy chọn của bạn và sau đó nhấp vào nó. Giả sử, nếu bạn đã nhấp vào nút có, thì kết quả sẽ như sau
Nếu bạn nhấp vào không có nút nào, nó sẽ gọi kết thúc () và nó sẽ đóng ứng dụng của bạn.

Không có nhận xét nào:
Đăng nhận xét