Android Switch开关、ToggleButton开灯关灯、Spinner下拉框实现开灯关灯图片的切换以及下拉框的使用

MainActivity8.java

package com.example.myapplication_one;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.List;


public class MainActivity8 extends AppCompatActivity {

    ToggleButton toggleButton1;
    ImageView imageView1;
    Spinner spinner1;
    //ListView listView1;
    String[] strings = new String[]{"济南","威海","泰安"};
    List<Student> list = new ArrayList<Student>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //用于加载页面布局   R.layout.名称
        setContentView(R.layout.layout8);
        init();
        /*
         * content:上下文
         * resource:每一个选项显示样式,
         * objects:数据源
         */
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity8.this
                        ,R.layout.support_simple_spinner_dropdown_item,strings);

        spinner1.setAdapter(arrayAdapter);

        spinner1.setPrompt("所在城市");//设置标题文字
        spinner1.setSelection(1);//设置显示第几item,默认是0,这是从0~n个数值    (设置当前选中哪项)
        //设置列表选中的事件监听
        //spinner1.setOnItemSelectedListener();是spinner1的监听器,如果item被选中了之后,会自动触发当前监听器
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {// spinner1自己拥有的监听器
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //第三个参数是选中选项的下标
                Toast.makeText(MainActivity8.this,
                        "您选择的城市为:"+strings[arg2],
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {//当没选中任何选项的话,触发这个方法
                Toast.makeText(MainActivity8.this ,
                        "请选中您所在城市",
                        Toast.LENGTH_SHORT).show();
            }
        });

//        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//                //第三个参数是选中选项的下标
//               // Student stu = list.get(arg2);
//            }
//        });

        /*
         * ToggleButton的监听器:setOnCheckedChangeListener
         * 匿名内部类:new OnCheckedChangeListener()
         * 重写OnCheckedChanged
         * CompoundButton arg0:出发当前监听器的控件
         * boolean arg1:当前toggleButton的checked属性值
         */

        //设置 toggleButton1监听
        toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton,boolean ischecked) {
                if (ischecked){
                    imageView1.setImageResource(R.drawable.word);
                }else{
                    imageView1.setImageResource(R.drawable.ppt);
                    imageView1.setBackgroundResource(R.drawable.a1);//修改背景资源
                }
//                ischecked ? imageView1.setImageResource(R.drawable.head)
//                    : imageView1.setImageResource(R.drawable.choose);
            }
        });
    }


    private void init() {
         toggleButton1 =findViewById(R.id.toggleButton1);
         imageView1 = findViewById(R.id.imageView1);
         spinner1 = findViewById(R.id.spinner1);
    }


}

layout8.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch" />


    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:textOn="关灯"
        android:textOff="开灯"
        android:checked="false"/><!--默认是关灯,false-->

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/a1"
        android:layout_marginLeft="100dp"/>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"/> 



</RelativeLayout>

结果:

反复点击开关按钮观察其变化:

我们发现,反复点击开灯按钮会实现图片的一个更换。

说明一点:
在上面,我们是使用了适配器去设置的spinner下拉框中的内容的,还有一种方法,也能够去设置spinner下拉框中的内容。如下:

首先,在values下创建一个values XML File:

我把这个文件命名为:values_layout1.xml文件,其中的内容为:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="spinner_data">
        <item>济南</item>
        <item>青岛</item>
        <item>威海</item>
    </string-array>
</resources>

然后对layout8.xml中修改:(只是在Spinner下加了一条语句)

 <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:entries="@array/spinner_data" /> <!--写了这个就不用适配器了,但是这个用法很不灵活--><!--    这个写在了values下    -->


然后加下来就是在Activity8中设置代码:(就是把适配器删除即可)

看效果:

文字信息已经更改过来了,但是城市还是提示威海,那是因为没有删除strings:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值