安卓基础知识

一、边距
外边距:margin
内边距:padding
二、点击事件
主类实现View.OnClickListener,重写OnClick(View view)
使用button的setOnClickListener方法进行绑定,传入this
新建一个内部类,实现View.OnClickListener,重写OnClick(View view)方法
使用button的setOnClickListener方法进行绑定,传入内部类对象
匿名内部类
在布局文件使用onCheck属性绑定监听函数
三、基本控件
3.1 Radiobutton:单选框
必须包含于RadioGroup中
android:checked:设置默认属性
在java代码中使用RadioGroup的check(控件id)来进行选择

3.2 CheckBox:多选框
android:checked:设置默认属性
3.3 PrograssBar
3.3.1 控件常用属性
style:设置进度条样式
style="?android:attr/progressBarStyleHorizontal"
android:progress:设置默认进度
android:indeterminate=“true”:让进度条一直滚动
android:max:设置进度条的最大值
android:visibility
gone:消失不显示
invisible:隐形
3.3.2 对象常用属性、方法
setProgress()
四、常见对话框
4.1 普通对话框
alertDialog = new AlertDialog.Builder(this)
.setTitle(“普通对话框”) //标题
.setMessage(“确定要退出吗?”) //提示信息
.setNegativeButton(“取消”,null) //负极
.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {//正极
@Override
public void onClick(DialogInterface dialog, int which) {
//确定点击事件
finish();
}
}).create();
alertDialog.show();
1
2
3
4
5
6
7
8
9
10
11
12
效果

4.2 单选对话框
final TextView show = findViewById(R.id.single_show);
final String[] sex = new String[]{“男”,“女”}; //传入单选框数据
alertDialog = new AlertDialog.Builder(this)
.setTitle(“普通对话框”) //标题
//(元素,默认,监听)
.setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,sex[which],Toast.LENGTH_SHORT).show();
show.setText(“性别:”+sex[which]);
}
}).setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //取消对话框
}
}).create();
alertDialog.show();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
效果

4.3 多选对话框
final TextView show = findViewById(R.id.multiple_show);
final String[] hobby = new String[]{“娱乐”,“文学”,“旅游”,“二次元”}; //多选元素
final boolean[] flag = new boolean[]{false,false,false,false}; //标准数组
alertDialog = new AlertDialog.Builder(this)
.setTitle(“多选对话框”) //标题
.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String Choice = “”; //显示在屏幕
for(int i=0; i<flag.length; i++){
if(flag[i] == true){ //扫描标志位
Choice += hobby[i]+"\n";
}
}
show.setText(Choice);
dialog.dismiss(); //取消对话框
}
})
.setMultiChoiceItems(hobby, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this,hobby[which]+isChecked,Toast.LENGTH_SHORT).show();
flag[which] = isChecked; //设置对应的标志位
}
}).create();
alertDialog.show();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
效果

4.4 进度条对话框
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle(“正在加载,请稍后…”);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new Thread(()->{
for(int i=0; i<100; i++){
progressDialog.setProgress(i);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}).start();
progressDialog.show();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
效果

4.5 自定义对话框
设置布局(dialog_layout.xml)

设置style(style.xml)

1
2
3
自定义Dialog类(继承Dialog,为相关按钮设置点击事
package com.example.mydialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

public class MyDialog extends Dialog {
public MyDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
setContentView(R.layout.dialog_layout);
findViewById(R.id.yes).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getOwnerActivity().finish();
}
});
findViewById(R.id.no).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
显示(MyDoalog.show())
MyDialog myDialog = new MyDialog(MainActivity.this,R.style.dialog);
myDialog.show();
1
2
效果

————————————————
版权声明:本文为CSDN博主「梦视空」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_44748994/article/details/105531487

猜你喜欢

转载自blog.csdn.net/CChuXueZhe2034/article/details/106966662
今日推荐