Android基本控件的使用:Spinner(下拉框)& TimePicker(钟表)

一,Spinner(下拉框)

1.在页面中添加Spinner控件
Android基本控件的使用:Spinner(下拉框)& TimePicker(钟表)_第1张图片

2.写java代码测试运行
先用ArrayAdapter往下拉框中存值
然后设置OnItemSelectedListener监听事件,检测打印输出

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

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

public class Main3Activity extends AppCompatActivity {
   private Spinner spinner;
   List list=new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        spinner= (Spinner) findViewById(R.id.spinner);
        ArrayAdapter arrayAdapter=new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,getList());
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                String info=spinner.getItemAtPosition(position).toString();
                Toast.makeText( Main3Activity.this,"点击了下拉框"+info,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });
    }
    public List getList(){
        list.add("辽宁");
        list.add("吉林");
        list.add("黑龙江");
        return list;
    }
}

二,TimePicker(钟表)

1.在页面中添加TimePicker控件
Android基本控件的使用:Spinner(下拉框)& TimePicker(钟表)_第2张图片
2.写java代码测试运行
设置OnTimeChangedListener监听事件,检测打印输出
代码如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;


public class Main3Activity extends AppCompatActivity {

    private TimePicker timePicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
      
        timePicker= (TimePicker) findViewById(R.id.timePicker);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(Main3Activity.this,"点击了"+hourOfDay+":"+minute,Toast.LENGTH_SHORT).show();
            }
        });                
    }
}

你可能感兴趣的:(Andiord)