Android开发如何创建自定义Dialog

本文介绍了在Android开发中创建自定义Dialog的步骤,包括创建自定义View并在Dialog中显示。以一个具体的例子——TextDialog——展示了如何在自定义View的构造函数中构建布局,包含一个EditText用于输入文字,一个SeekBar用于选择文字大小,以及确定和取消按钮。同时,实现了 SeekBar 的监听功能,当用户选择完成后,会回调监听器传递输入的文字和文字大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Android开发中创建自定义Dialog的关键只有两个步骤:

  1.创建一个需要在自定义Dialog显示的自定义View,创建这个自定义View时,既可以在这个View的构造方法中用Android开发自带的控件把自定义View构造出来;也可以在自定义View@Override protected void onDraw(Canvas canvas)方法中,把自定义View绘制出来

  2.在自定义Dialog@Override protected void onCreate(Bundle savedInstanceState)方法中,通过setContentView(自定义View);方法,把我们的自定义View显示出来

  在创建好自定义Dialog后,我们在别的类中,只要调用自定义Dialog的构造函数就可以把自定义Dialog显示出来。对于ColorPickerDialog这个类,调用语句如下:new ColorPickerDialog(getContext(), listener, mPaint.getColor()).show();

  下面提供一个Android开发在自定义View的构造函数中把View构造出来的例子:

  /**

  文字对话框

  */

  public class TextDialog extends Dialog implements SeekBar.OnSeekBarChangeListener{

  private LinearLayout linearLayout;

  private EditText etForText;

  private SeekBar seekBar;

  private TextView tvForSeekBar;

  private Button btnOk;

  private Button btnCancel;

  private LinearLayout topChildLinearLayout;

  private LinearLayout bottomChildLinearLayout;

  private OnTextInputListener mListener;

  /**

  文字对话框标题

  */

  private String title = "请输入文字与选择文字大小";

  public interface OnTextInputListener {

  void textInput(String text, int textSize);

  }

  public TextDialog(Context context, OnTextInputListener listener)

  {

  super(context);

  mListener = listener;

  linearLayout = new LinearLayout(getContext());

  linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

  linearLayout.setOrientation(LinearLayout.VERTICAL);

  linearLayout.setGravity(Gravity.CENTER);

  etForText = new EditText(context);

  etForText.setMinLines(5);//设置最大行数

  seekBar = new SeekBar(context);

  seekBar.setLayoutParams(new LinearLayout.LayoutParams(200, LayoutParams.FILL_PARENT));

  seekBar.setMax(100);

  seekBar.setProgress(50);

  seekBar.setOnSeekBarChangeListener(this);

  tvForSeekBar = new TextView(context);

  tvForSeekBar.setText("50");

  btnOk = new Button(getContext());

  btnOk.setText("确定");

  btnCancel = new Button(getContext());

  btnCancel.setText("取消");

  linearLayout.addView(etForText);

  topChildLinearLayout = new LinearLayout(getContext());

  topChildLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  topChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

  topChildLinearLayout.addView(seekBar);

  topChildLinearLayout.addView(tvForSeekBar);

  linearLayout.addView(topChildLinearLayout);

  bottomChildLinearLayout = new LinearLayout(getContext());

  bottomChildLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  bottomChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

  bottomChildLinearLayout.setGravity(Gravity.CENTER);

  bottomChildLinearLayout.addView(btnOk);

  bottomChildLinearLayout.addView(btnCancel);

  linearLayout.addView(bottomChildLinearLayout);

  btnOk.setOnClickListener(new View.OnClickListener()

  {

  @Override

  public void onClick(View v)

  {

  // TODO Auto-generated method stub

  //验证text是否为空

  String text = etForText.getText().toString()/*.replace("\n", "")*/;

  if(text == null || text.trim().equals(""))

  {

  Toast.makeText(getContext(), "文字不能为空", Toast.LENGTH_SHORT).show();

  return;

  }

  int textSizeInt = Integer.valueOf(tvForSeekBar.getText().toString());

  mListener.textInput(text, textSizeInt);

  dismissDialog();

  }

  });

  btnCancel.setOnClickListener(new View.OnClickListener()

  {

  @Override

  public void onClick(View v)

  {

  // TODO Auto-generated method stub

  dismissDialog();

  }

  }

  }

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(linearLayout);

  setTitle(title);

  }

  public void dismissDialog()

  {

  this.dismiss();

  }

  @Override

  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)

  {

  // TODO Auto-generated method stub

  tvForSeekBar.setText(progress + "");

  }

  @Override

  public void onStartTrackingTouch(SeekBar seekBar

  {

  // TODO Auto-generated method stub

  }

  @Override

  public void onStopTrackingTouch(SeekBar seekBar)

  {

  // TODO Auto-generated method stub

  }

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值