android实践项目一实现简单的验证码和spinner下拉选项效果

android练习demo1

Textview1

类似验证码的效果 点击之后生成一个随机的4位数,更换颜色。
代码如下

public class MainActivity extends Activity {
    private TextView text1;
    private Button btn1;
    private int color[] = { Color.BLUE, Color.BLACK, Color.GREEN, Color.RED };
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if(msg.what==1){
              //核心代码如下Math.random();和Random().nextInt();
                text1.setText((int)(Math.random()*9000+1000)+"");
                text1.setTextColor(color[new Random().nextInt(4)]);
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text1 = (TextView) findViewById(R.id.text1);
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new Thread() {
                    public void run() {

                        init();
                    };

                }.start();
            }
        });
    }

    private void init() {
        // TODO Auto-generated method stub
        Message msg = new Message();
        msg.what = 1;
        handler.sendMessage(msg);

    }

}

android 练习demo2

spinner

下拉列表框中的列表项主要有以下两种配置方式。

方式一、通过资源文件配置,例如定义一个values\city_data.xml的文件,在定义数据内容时需要使用元素指定,定义内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="city_items">
        <item >1</item>
        <item >2</item>
        <item >3</item>
        <item >4</item>
        <item >5</item>
    </string-array>
</resources>

方式二、通过android.widget.ArrayAdapter类读取资源文件或者指定具体的数据。

定义布局xml文件

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.demotest.MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择你喜欢的数字" />

    <Spinner
        android:id="@+id/sp1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/city_items"/> <!-- 加载数据到spinner中 -->

</LinearLayout>
public class SpinnerText3 extends Activity {
    private Spinner spintener;
    private Spinner spintener2;
    private TextView textView;
    private List<String> data = null;
    private ArrayAdapter<String> adapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // 生命周期方法
        super.setContentView(R.layout.spinner2); // 设置要使用的布局管理器
        textView = (TextView) findViewById(R.id.text2);
        spintener = (Spinner) findViewById(R.id.sp1);
        spintener2 = (Spinner) findViewById(R.id.sp2);
        spintener2.setPrompt("选择国籍");
        data = new ArrayList<String>();
        data.add("中国");
        data.add("美国");
        data.add("日本");
        data.add("韩国");
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, data);
        spintener2.setAdapter(adapter);

        // 加载数据
        spintener.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 拿到数组
                String[] cities = getResources().getStringArray(R.array.city_items);
                // 根据位置显示数据
                textView.setText("您选择的数字是" + cities[position]);
            }

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

            }

        });

        spintener2.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                String[] str = data.toArray(new String[data.size()]);
                Toast.makeText(SpinnerText3.this, "帅哥" + str[position], Toast.LENGTH_SHORT).show();;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });
    }
posted @ 2016-04-12 20:43  Tesi1a  阅读(126)  评论(0编辑  收藏  举报