用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - ios代码库

handler随机颜色

2017-03-10 作者:老四和老三的老二都老大了举报

[ios]代码库

private TextView customView;
    private int count=0;
    private boolean flag = false;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                customView.setText(mList.get(count));
                customView.setBackgroundColor(Color.parseColor(mList.get(count)));
                if(count<mList.size()-1)
                    count++;
                else
                    count=0;
                handler.sendEmptyMessageDelayed(0,1000);
            }
        }
    };
    private List<String> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Random random=new Random();
        customView = (TextView) findViewById(R.id.customView);

        customView.setTextSize(30);
        //初始化数据
        mList = new ArrayList<>();
        /*mList.add("#6187c5");
        mList.add("#FFFFFF");
        mList.add("#BFBFBF");
        mList.add("#6699FF");
        mList.add("#FF9900");
        mList.add("#CC0000");*/
        for(int i=0;i<50;i++){
            String chars = "abcdef";
            char a=chars.charAt((int)(Math.random()*6));
            char b=chars.charAt((int)(Math.random()*6));
            int c=random.nextInt(10);
            int d=random.nextInt(10);
            int e=random.nextInt(10);
            int f=random.nextInt(10);
            String ss="#"+a+b+c+d+e+f;
            mList.add(ss);
        }
        customView.setText(mList.get(count));
        customView.setBackgroundColor(Color.parseColor(mList.get(count)));

        handler.sendEmptyMessageDelayed(0,1000);
        flag=true;

        customView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(flag){
                    flag=false;
                    Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show();
                    handler.removeMessages(0);
                }else{
                    flag=true;
                    Toast.makeText(MainActivity.this, "开启", Toast.LENGTH_SHORT).show();
                    handler.sendEmptyMessageDelayed(0,1000);
                }

            }
        });
    }


分享到:
更多

网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。