Android Studio 实战干货例程

Android Studio 例程

android studio 3.3.2版本的开发环境

      讲解一些基本控件Spinner 下拉列表的控件,

初学阶段的童靴们看过来。

      首先创建MainActivity视图
      直接代码拉过来
// An highlighted block
   public class SpinnerDropdownActivity extends AppCompatActivity {
     
    //声明数组
    private String[] germsArray = {"病毒", "阮病毒", "细菌", "蓝藻", "放线菌", "真菌","植物"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_dropdown);

        //数组适配器
        ArrayAdapter<String> germsAdapt = new ArrayAdapter<String>(this,
               R.layout.item_select, germsArray);
        germsAdapt.setDropDownViewResource(R.layout.item_dropdown);

        Spinner sp = findViewById(R.id.sp_dropdown);
        sp.setPrompt("请选择生物");
        sp.setAdapter(germsAdapt);
        sp.setSelection(0);
        sp.setOnItemSelectedListener(new MySelectedListener());
    }

    public class MySelectedListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(SpinnerDropdownActivity.this,"您选择的是"+germsArray[position],Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    }
}

创建两个布局文件

1. item_select.xml

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tv_name"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:singleLine="true"
  android:textColor="#0000ff"
  android:textSize="17sp" />

2.item_dropdown.xml

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_name"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="#ff0000"
    android:textSize="17sp" />

在这里插入图片描述
这是新手道路上的实战者1

下面加入图标的下拉列表

用到SimpleAdapt类
看代码

// An highlighted block
public class SpinnerIconActivity extends AppCompatActivity {

    private Spinner mspinner;
    private String[] arrayname = {"病毒", "阮病毒", "细菌", "蓝藻", "放线菌", "真菌", "植物"};
    private int[] arrayIcon = {
            R.mipmap.germs, R.mipmap.rgerms, R.mipmap.xijun, R.mipmap.lanzao,
            R.mipmap.fangxianjun, R.mipmap.zhengjun, R.mipmap.zhiwu};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_icon_spinner);
        mspinner = findViewById(R.id.Icon_spinner_page);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < arrayIcon.length; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("icon", arrayIcon[i]);
            item.put("name", arrayname[i]);
            list.add(item);
        }
        
        SimpleAdapter simpleAdapter = new SimpleAdapter(SpinnerIconActivity.this,
                list, R.layout.item_spinnericon, new String[]{"icon", "name"}, new int[]{
                R.id.iv_icon, R.id.tv_name
        });
        mspinner.setPrompt("请选择生物");
        mspinner.setSelection(0);
        mspinner.setAdapter(simpleAdapter);
        mspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(SpinnerIconActivity.this,"您选择的是"+arrayname[position],Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

这是Activity布局文件

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Spinner
        android:id="@+id/Icon_spinner_page"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dialog">
    </Spinner>
    
</LinearLayout>

这是SimpleAdapt中的适配器布局文件

// An highlighted block
<?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">
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="120dp"
        android:layout_height="120dp" />
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginRight="15dp"
        android:textSize="30sp"
        android:textColor="@color/colorAccent"
        android:gravity="center"
        android:textAlignment="viewEnd"
        />
</LinearLayout>
看效果图

在这里插入图片描述
在这里插入图片描述
Authors
: Chase
: Zhou


  1. 看看上面的代码是不是很简单易懂好实践 ↩︎

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值