創(chuàng)建自定義的AlertDialog可以通過多種方式實現(xiàn),具體取決于你使用的開發(fā)環(huán)境和框架。以下是一些常見的方法:
在Android開發(fā)中,你可以通過繼承Dialog
類來創(chuàng)建自定義的AlertDialog。以下是一個簡單的示例:
Dialog
:import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CustomAlertDialog extends Dialog {
public CustomAlertDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_custom);
Button btnPositive = findViewById(R.id.btnPositive);
Button btnNegative = findViewById(R.id.btnNegative);
EditText input = findViewById(R.id.editText);
btnPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = input.getText().toString();
// 處理用戶輸入
dismiss();
}
});
btnNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
dialog_custom.xml
:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter some text"/>
<Button
android:id="@+id/btnPositive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Positive"/>
<Button
android:id="@+id/btnNegative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Negative"/>
</LinearLayout>
CustomAlertDialog customAlertDialog = new CustomAlertDialog(this);
customAlertDialog.show();
在Flutter中,你可以使用showDialog
函數(shù)來顯示一個自定義的AlertDialog。以下是一個簡單的示例:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Custom AlertDialog')),
body: Home(),
),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => CustomAlertDialog(),
);
},
child: Text('Show AlertDialog'),
),
);
}
}
class CustomAlertDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('Custom AlertDialog'),
content: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Enter some text:'),
SizedBox(height: 16),
TextField(
decoration: InputDecoration(border: OutlineInputBorder()),
),
],
),
actions: <Widget>[
TextButton(
child: Text('Positive'),
onPressed: () {
Navigator.of(context).pop('Positive');
},
),
TextButton(
child: Text('Negative'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
}
}
以上示例展示了如何在原生Android和Flutter中創(chuàng)建自定義的AlertDialog。你可以根據自己的需求進行調整和擴展。