概述
先上图吧:
今天下午难得的有空自己写点东西,想起来之前看到手机上的时间选择器,当时就想写,但是就是没有时间;
这个小程序可以实现,滑动输入密码,然后会出现一个开锁的土司,当然后期也可以让他开机运行,输入密码之后在可以进入系统等等;
这个小玩意儿挺简单的,可以说没一点难度,要不还是先看代码吧:
布局代码:
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.smile_raccoon.somedemos3.MainActivity">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number_shi"
android:layout_alignTop="@+id/number_fen"
android:layout_toLeftOf="@+id/textView2"
android:layout_toStartOf="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#E61A5F"
android:text="时"
android:id="@+id/textView2"
android:layout_alignTop="@+id/textView3"
android:layout_toLeftOf="@+id/number_fen"
android:layout_toStartOf="@+id/number_fen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#E61A5F"
android:text="分"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView"
android:layout_toLeftOf="@+id/number_miao"
android:layout_toStartOf="@+id/number_miao" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number_miao"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/textView"
android:layout_toStartOf="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#E61A5F"
android:text="秒"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/textView" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number_fen"
android:layout_alignTop="@+id/number_miao"
android:layout_toLeftOf="@+id/textView3"
android:layout_toStartOf="@+id/textView3" />
<Button
android:id="@+id/submit_button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="-------解锁------"
android:background="#E61A5F"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java代码:
<pre name="code" class="java">package com.example.numberpickerdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.NumberPicker;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private NumberPicker number_shi;
private NumberPicker number_fen;
private NumberPicker number_miao;
private Button submit_button;
private String choose = "2";
private String[]shiString;
private String[]fenString;
private String[]miaoString;
private int shiBoolean = 1;
private int fenBoolean;
private int miaoBoolean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initDate();
initLinstener();
}
/**
* 初始化布局
*/
private void initView() {
number_shi = (NumberPicker) findViewById(R.id.number_Shi);
number_fen = (NumberPicker) findViewById(R.id.number_Fen);
number_miao = (NumberPicker) findViewById(R.id.number_Miao);
submit_button = (Button) findViewById(R.id.submit_button);
}
/**
* 初始化数据
*/
private void initDate() {
shiString = new String[24];
if(choose.equals("1")){
for(int a = 1;a<=24;a++){
shiString[a-1] = a+"";
}
number_shi.setMaxValue(24);
}else if(choose.equals("2")){
for(int a = 1;a<=12;a++){
shiString[a-1] = a+"";
}
number_shi.setMaxValue(12);
}
number_shi.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
number_shi.setMinValue(1);
fenString = new String[60];
for(int a = 0;a<60;a++){
fenString[a] = a+"";
}
number_fen.setDisplayedValues(fenString);
number_fen.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
number_fen.setMaxValue(59);
number_fen.setMinValue(0);
miaoString = new String[60];
for(int a = 0;a<=59;a++){
miaoString[a] = a+"";
}
number_miao.setDisplayedValues(miaoString);
number_miao.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
number_miao.setMaxValue(59);
number_miao.setMinValue(0);
}
/**
* 初始化监听
*/
private void initLinstener() {
number_shi.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
shiBoolean = number_shi.getValue();
Log.i("stringShi---oldVal",shiBoolean+"");
}
});
number_fen.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
fenBoolean = number_fen.getValue();
Log.i("stringFen---oldVal",fenBoolean+"");
}
});
number_miao.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
miaoBoolean = number_miao.getValue();
Log.i("stringFen---oldVal",miaoBoolean+"");
Log.i("stringMiao---oldVal",newVal+"");
}
});
submit_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("stringMiao---oldVal",shiBoolean+"");
Log.i("stringMiao---oldVal",fenBoolean+"");
Log.i("stringMiao---oldVal",miaoBoolean+"");
if(shiBoolean == 1 && fenBoolean == 2 && miaoBoolean == 3 ){
Toast.makeText(MainActivity.this,"开锁!",Toast.LENGTH_LONG).show();
return;
}else{
Toast.makeText(MainActivity.this,"密码错误!",Toast.LENGTH_LONG).show();
return;
}
}
});
}
}
只需要记住这几点
1,显示小时的数组
2,显示分钟的数组
以上两项一定要记住,是数组 数组 数组,不然在后面转换的比较麻烦
3,new出来NumberPicker之后需要设置四个:
a,setDisplayedValues(设置想显示的数据数组)
b,setDescendantFocusability(设置不能手动输入数据)
c,setMaxValue(设置最大值,就是你的数组的最大的那个下标)
d,setMinValue(设置最小值,一般为0)
最后
以上就是活泼铃铛为你收集整理的NumberPicker 很炫酷,很简单的控件的全部内容,希望文章能够帮你解决NumberPicker 很炫酷,很简单的控件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复