前言
本博文主要是对Android studio中spinner控件的一个介绍,以及记录,方便后续使用时候可以直接加载。
spinner介绍
Spinner是Android开发中常用的UI控件之一,它是一个下拉列表,可以用来显示多个选项,用户可以通过点击下拉箭头选择其中的一项。Spinner通常用于用户选择一个选项的场景,比如选择性别、选择城市、选择日期等。
Spinner的使用步骤如下:
- 在布局文件中定义Spinner控件,如:
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- 在Java代码中找到Spinner控件,并为其设置适配器,如:
Spinner spinner = findViewById(R.id.spinner);
// 创建一个适配器,并将数据绑定到适配器上
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, data);
// 设置下拉列表的样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 将适配器设置给Spinner
spinner.setAdapter(adapter);
- 设置Spinner的选中监听器,监听用户选择的选项,如:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// 获取用户选择的选项数据
String selectedItem = (String) parent.getItemAtPosition(position);
// 处理用户选择的选项
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// 当没有选项被选中时的处理逻辑
}
});
通过以上步骤,就可以在Android Studio中使用Spinner来展示和处理用户的选择了。
示例
静态spinner:
在res/values下编写一个:myarrays.xml的文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="data">
<item>英勇青铜</item>
<item>不屈白银</item>
<item>荣耀黄金</item>
<item>华贵铂金</item>
<item>璀璨钻石</item>
<item>超凡大师</item>
<item>最强王者</item>
</string-array>
</resources>
接着是第二个Spinner的布局:item_spin_hero.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="match_parent"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/img_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@mipmap/iv_lol_icon1" />
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="德玛西亚"
android:textSize="16sp" />
</LinearLayout>
//android:prompt:设置对话框模式的列表框的提示信息(标题),只能够引用string.xml 中的资源id,而不能直接写字符串
<Spinner
android:id="@+id/spin_one"
android:layout_width="100dp"
android:layout_height="64dp"
android:entries="@array/data"
android:prompt="@string/spin_title"
android:spinnerMode="dialog" />
spin_one = (Spinner) findViewById(R.id.spin_one);
spin_one.setOnItemSelectedListener(this);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.spin_one:
//内容
break;
}
}
动态spinnner:
<Spinner
android:layout_width="match_parent"
android:layout_height="50dp"
android:spinnerMode="dropdown"
android:layout_marginLeft="10dp"
android:id="@+id/spinner">
private ArrayList<String> spinner_data;
private ArrayAdapter<String> adapter_machine;
spinner=mView.findViewById(R.id.spinner);
getspinner();
spinner.setOnItemSelectedListener(this);
private void getspinner() {
Map<String, Object> map = new HashMap<>();
map.put("user_name", SharedPreferencesUtil.getString(getActivity(), "user_name"));
String Json = JSON.toJSONString(map);
map.clear();
RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), Json);
okHttpClient = new OkHttpClient();
request = new Request.Builder()
.url(IpURL.URL() + IpURL.FileName() + "alarm_spinner.php")
.post(requestBody)
.build();
//创建/Call
Call call = okHttpClient.newCall(request);
//加入队列 异步操作
call.enqueue(new Callback() {
//请求错误回调方法
@Override
public void onFailure(Call call, IOException e) {
System.out.println("连接失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//Log.v("data",json.getString("result"));
Message message = new Message();
message.what = 1;
message.obj = response.body().string();
mhandler.sendMessage(message);
}
});
}
Handler mhandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
json = JSON.parseObject(msg.obj.toString());
if (json.getString("result").equals("true")) {
spinner_data=new ArrayList<>();
jsonArray_data1 = JSON.parseArray(json.getString("data"));
for (int i=0;i<jsonArray_data1.size();i++) {
json_data = jsonArray_data1.getJSONObject(i);
String temp=json_data.getString("type");
//采用equals,不用==
if (temp .equals("micro")){
spinner_data.add("S系列主机监控系统");
}
}
if(adapter_machine==null){
adapter_machine= new ArrayAdapter<>(getActivity(), R.layout.view_spinner_item, spinner_data);
adapter_machine.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter_machine);
}else{
adapter_machine.notifyDataSetChanged();
}
} else {
Toast.makeText(getActivity(), json.getString("message"), Toast.LENGTH_SHORT).show();
}
}
}
};
额外补充
需求实现部分就先到这!接下来,是一些个人平时整理的c++要点概要,如有需要可以进行阅读!
结构体
- 第一种是最基本的结构体定义, 其定义了一个结构体 A。
struct A { int a;};
- 第二种则是在定义了一个结构体 B 的同时定义了一个结构体 B 的变量 m。
struct B { int b;}m;
- 第三种结构体定义没有给出该结构体的名称, 但是定义了一个该结构体的变量 n, 也就是说,若是想要在别处定义该结构体的变量是不行的, 只有变量 n 这种在定义结构体的同时定义变量才行。
struct { int c;}n;
- 第四种结构体定义在第一种结构定义的基础上加了关键字
typedef
, 此时我们将struct D{int d}
看成是一个数据类型, 但是因为并没有给出别名, 直接用 D 定义变量是不行的。 如D test;
,不能直接这样定义变量 test。 但struct D test;
可行。
typedef struct D { int d;};
- 第五种结构体定义在第四种结构体定义的基础上加上了别名 x, 此时像在第四种结构体定义中说得那样, 此时的结构体 E 有别名 x, 故可以用 x 定义 E 的结构体变量。 用 E 不能直接定义, 需要在前面加
struct,
如struct E test;
。
typedef struct E { int e;}x;
- 第六种结构体定义在第五种的基础上减去了结构体名, 但是若是直接使用 y 来定义该结构体类型的变量也是可以的。 如
y test;
。 (常用)
typedef struct { int f;}y;
使用内联函数的时候要注意:
函数重载
- C++函数重载的判断依据
- 函数形参的个数;
- 函数形参的类型;
- 形参的顺序;
- 不能把返回值作为函数重载的条件, 原因是编译器在编译时不会去判断函数的返回类型,函数只有调用后, 编译器才会去验证返回类型, 所以返回值不能作为函数重载的依据;
- 常函数 const 可以作为函数重载判断依据, 原因: 重载[]运算符时, 有没有 const 的区别是: 有 const 只读, 没有 const 读写都可以。
- 注意事项:
- 形参的个数或类型至少要有一项是不同的;
- 编译器不以返回值来区分函数。
虚函数和纯虚函数
纯虚函数是指在 C++中, 函数可以用“virtual”和“= 0”关键字来声明。 该函数是一个虚函数,但是在声明时, 没有定义它的函数体。 它是一个空函数, 只提供函数声明, 而不提供实际实现。 由于它编译器不能确定如何实现, 因此需要在子类中实现它。 因此, 纯虚函数也被称为抽象函数。 形如:
class <类名>
{
virtual <类型><函数名>(<参数表>)=0;
…
};
虚函数和纯虚函数的特性:
- 虚函数可以在类中声明, 也可以在类的外部声明, 编译器会自动将它们转换为虚函数;但是纯虚函数只能在类中声明, 而不能在类的外部声明。
- 虚函数可以有实现, 也可以没有实现; 而纯虚函数没有实现, 不可以有实现。
- 虚函数可以在子类中覆盖, 也可以不被覆盖; 而纯虚函数必须在子类中覆盖, 否则编译器将报错。
- 虚函数可以被多态调用, 也可以被静态调用; 而纯虚函数只可以被多态调用, 不可以被静态调用。