APP学习4(Toast、对话框简单单选多选)
1.Toast
Toast是Android系统提供的轻量级信息提醒制度,用于向用户提示即时信息,它显示在引用程序界面的最上层,显示一段时间后自动消失,不会打断当前操作,也不获得焦点。
Toast.makeText(MainActivity.this,"WIFI已断开",Toast.LENGTH_SHORT).show();
2.AlertDialog对话框
AlertDialog对话框用于提示一些主要信息或者显示以下需要用户额外交互的内容。一般以小窗口的形式展示在界面上。使用AlertDialog创建的对话框一般包含标题、内容和按钮三个区域。
2.1 简单对话框
代码:
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="退出" android:id="@+id/exitBtn" android:onClick="myclick" ></Button>
MainActivity.java
package com.example.myapp; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void myclick(View view){ //点击触发运行对话框 /* * 1.创建对话框的builder对象,构建对话 * builder对象调用creat()方法创建对话框对象,show()方法调用运行 * */ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher) .setTitle("退出") .setMessage("确定要退出吗") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss();//关闭当前对话框 MainActivity.this.finish();//关闭程序 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss();//只需要关闭当前对话框 } }) ; AlertDialog alertDialog=builder.create(); alertDialog.show(); } }
2.2 单选对话框
编号是从0开始的。
代码:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="信息提示" android:textSize="20sp" android:gravity="center" android:id="@+id/text" ></TextView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置字体大小" android:id="@+id/exitBtn2" android:onClick="myclick2"> </Button>
MainActivity.java
package com.example.myapp; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=findViewById(R.id.text); } int index=0; public void myclick2(View view){ //点击触发运行对话框 /* * 1.创建对话框的builder对象,构建对话 * builder对象调用creat()方法创建对话框对象,show()方法调用运行 * */ String[] mags=new String[]{"小号","中号","大号","超大号"}; int[] textsize = new int[]{10,20,30,40};//文本尺寸 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher) .setTitle("设置文本大小") .setSingleChoiceItems(mags, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { index = i; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { textView.setTextSize(textsize[index]); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }) ; AlertDialog alertDialog=builder.create(); alertDialog.show(); } }
2.3 多选框
从0开始。
代码:
package com.example.myapp; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=findViewById(R.id.text); } int index=0; public void myclick2(View view){ //点击触发运行对话框 /* * 1.创建对话框的builder对象,构建对话 * builder对象调用creat()方法创建对话框对象,show()方法调用运行 * */ AlertDialog.Builder builder = new AlertDialog.Builder(this); String[] hobby = new String[]{"电影","旅游","音乐","看书"};//第一个参数:内容 boolean[] isCheck = new boolean[]{true,false,false,false};//第二个参数:选中状态 builder.setIcon(R.mipmap.ic_launcher) .setTitle("你的爱好") .setMultiChoiceItems(hobby, isCheck, new DialogInterface.OnMultiChoiceClickListener() { @Override //i:选中的序号,b:点击项的选中状态 public void onClick(DialogInterface dialogInterface, int i, boolean b) { isCheck[i] = b; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { StringBuffer sb = new StringBuffer(); for(int j=0;j<isCheck.length;j++){ if(isCheck[j]){ sb.append(hobby[j]).append("$$$"); } } Toast.makeText(MainActivity.this,sb,Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }) ; AlertDialog alertDialog=builder.create(); alertDialog.show(); } }
接上一次练习
多选框信息的传递。
MainActicity.java
package com.example.myapp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private CheckBox cb1,cb2,cb3; String msg1="",msg2="",msg3=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb1 = findViewById(R.id.cd1); cb2 = findViewById(R.id.cd2); cb3 = findViewById(R.id.cd3); CompoundButton.OnCheckedChangeListener occl = new CompoundButton.OnCheckedChangeListener() { @Override //传递:第一个:传递回checkbox对象;第二个:传递过来的是否被选上 public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(compoundButton.getId() == R.id.cd1){ if(b)//如果被选中 { msg1="电影"; }else{ msg1=""; } } if(compoundButton.getId() == R.id.cd2){ if(b)//如果被选中 { msg2="足球"; }else{ msg2=""; } } if(compoundButton.getId() == R.id.cd3){ if(b)//如果被选中 { msg3="游泳"; }else{ msg3=""; } } Toast.makeText(MainActivity.this, msg1+msg2+msg3, Toast.LENGTH_SHORT).show(); } }; cb1.setOnCheckedChangeListener(occl); cb2.setOnCheckedChangeListener(occl); cb3.setOnCheckedChangeListener(occl); } }
activity_main.xml
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱好:" android:layout_gravity="center_vertical" ></TextView> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cd1" android:text="电影"> </CheckBox> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cd2" android:text="足球"> </CheckBox> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cd3" android:text="游泳"> </CheckBox> </LinearLayout>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?