经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android?Studio使用自定义对话框效果
来源:jb51  时间:2022/5/18 8:42:51  对本文有异议

Android Studio基础使用自定义对话框,供大家参考,具体内容如下

兼容低版本的APP运行

第一步:新建新的空白activity,布局XML设置如下

该APP的启动界面activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? xmlns:app="http://schemas.android.com/apk/res-auto"
  4. ? ? xmlns:tools="http://schemas.android.com/tools"
  5. ? ? android:layout_width="match_parent"
  6. ? ? android:layout_height="match_parent"
  7. ? ? tools:context=".MainActivity">
  8. ?
  9. ? ? <Button
  10. ? ? ? ? android:id="@+id/btn_single"
  11. ? ? ? ? android:layout_width="wrap_content"
  12. ? ? ? ? android:layout_height="wrap_content"
  13. ? ? ? ? android:text="显示单选对话框"/>
  14. ? ? <Button
  15. ? ? ? ? android:id="@+id/btn_alert_customer"
  16. ? ? ? ? android:layout_width="wrap_content"
  17. ? ? ? ? android:layout_height="wrap_content"
  18. ? ? ? ? android:text="自定义对话框"/>
  19. ?
  20. </LinearLayout>

自定义的对话框item_alert_dialog的布局XML设置

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? xmlns:app="http://schemas.android.com/apk/res-auto"
  4. ? ? xmlns:tools="http://schemas.android.com/tools"
  5. ? ? android:layout_width="match_parent"
  6. ? ? android:layout_height="match_parent"
  7. ? ? tools:context=".MainActivity">
  8. ?
  9. ? ? <Button
  10. ? ? ? ? android:layout_width="wrap_content"
  11. ? ? ? ? android:layout_height="wrap_content"
  12. ? ? ? ? android:text="自定义Dialog界面!"
  13. ? ? ? ? />
  14. ? ? <TextView
  15. ? ? ? ? android:layout_width="250dp"
  16. ? ? ? ? android:layout_height="250dp"
  17. ? ? ? ? android:background="@mipmap/ic_launcher"/>
  18. ?
  19. </LinearLayout>

第二步:在MainActivity.java中绑定布局xml

第三步:对话框实现抽象方式

  1. package com.xwb.zdydhk;
  2.  
  3. import androidx.appcompat.app.AlertDialog;
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  13.  
  14. ? ? @Override
  15. ? ? protected void onCreate(Bundle savedInstanceState) {
  16. ? ? ? ? super.onCreate(savedInstanceState);
  17. ? ? ? ? setContentView(R.layout.activity_main);
  18. ? ? ? ? Button btn_single = findViewById(R.id.btn_single);
  19. ? ? ? ? btn_single.setOnClickListener(this);
  20. ? ? ? ? Button btn_alert_customer = findViewById(R.id.btn_alert_customer);
  21. ? ? ? ? btn_alert_customer.setOnClickListener(this);
  22. ? ? }
  23. ? ? @Override
  24. ? ? public void onClick(View v){
  25. ? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this);
  26. ? ? ? ? switch (v.getId()){
  27. ? ? ? ? ? ? case R.id.btn_single:
  28. ? ? ? ? ? ? ? ? builder.setTitle("单选对话框").setIcon(R.mipmap.ic_launcher).setSingleChoiceItems(new String[]{"中国", "德国", "日本"}, 0, new DialogInterface.OnClickListener() {
  29. ? ? ? ? ? ? ? ? ? ? @Override
  30. ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
  31. ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"选中的"+which,Toast.LENGTH_SHORT).show();
  32. ? ? ? ? ? ? ? ? ? ? }
  33. ? ? ? ? ? ? ? ? });
  34. ? ? ? ? ? ? ? ? break;
  35. ? ? ? ? ? ? case R.id.btn_alert_customer:
  36. ? ? ? ? ? ? ? ? //setView(R.layout.item_alert_dialog)为自定义的对话框,或图片等等
  37. ? ? ? ? ? ? ? ? builder.setTitle("自定义对话框").setIcon(R.mipmap.ic_launcher).setView(R.layout.item_alert_dialog);
  38. ? ? ? ? ? ? ? ? break;
  39. ? ? ? ? ? ? }
  40. ? ? ? ? AlertDialog ad = builder.create();
  41. ? ? ? ? ad.show();
  42. ? ? }
  43. }

第四步:运行APP效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号