学习TimePicker和DataPicker

学习网易云课堂的android课程,练习代码

课堂地址 http://study.163.com/course/courseLearn.htm?courseId=207001#/learn/video?lessonId=426042&courseId=207001

布局文件

android studio会提示在ScrollView中的TimePicker和DataPicker的layout_height为wrap_content



    
        
    
    
    
    
        
    


Activity代码


public class MainActivity extends AppCompatActivity {
    TimePicker mTimePicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTimePicker = (TimePicker) findViewById(R.id.my_timepicker);
        mTimePicker.setIs24HourView(true);
        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                makeTost("hour = " + hourOfDay + " min = " + minute);
            }
        });
    }

    private void makeTost(String body){
        Toast.makeText(this, body, Toast.LENGTH_SHORT).show();
    }
}


主要是设置OnTimeChangedListener监听用户的选择事件

效果图

学习TimePicker和DataPicker_第1张图片

学习TimePicker和DataPicker_第2张图片

你可能感兴趣的:(android,UI学习,android,ui)