zoukankan      html  css  js  c++  java
  • 使用SeekBar办Android调色板

    1、接口布局xml代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="当前值是:#000000" />
    
            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20px"
                android:width="100px"
                android:text="test" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="红:" />
    
            <SeekBar
                android:id="@+id/seekBar1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:max="255"
                android:padding="10px"
                android:progress="00" />
    
            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="0"
                android:ems="10" >
    
                <requestFocus />
            </EditText>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="绿" />
    
            <SeekBar
                android:id="@+id/seekBar2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="255"
                android:progress="0"
                android:layout_weight="1" />
    
            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="00"
                android:ems="10" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="蓝" />
    
            <SeekBar
                android:id="@+id/seekBar3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="255"
                android:progress="0"
                android:layout_weight="1" />
    
            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="00"
                android:ems="10" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    2、java代码:

    package com.jwy;
    
    import android.os.Bundle;
    import android.R.integer;
    import android.R.string;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.Menu;
    import android.widget.EditText;
    import android.widget.SeekBar;
    import android.widget.Toast;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    	private TextView textView;
    	private SeekBar seekBar1;
    	private SeekBar seekBar2;
    	private SeekBar seekBar3;
    	
    	private EditText editText1;
    	private EditText editText2;
    	private EditText editText3;
    	
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = (TextView)findViewById(R.id.textView1);
            seekBar1 =(SeekBar)findViewById(R.id.seekBar1);
            seekBar2 =(SeekBar)findViewById(R.id.seekBar2);
            seekBar3 =(SeekBar)findViewById(R.id.seekBar3);
            
            editText1=(EditText)findViewById(R.id.editText1);
            editText2=(EditText)findViewById(R.id.editText2);
            editText3=(EditText)findViewById(R.id.editText3);
            
            seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    		{
    			
    			@Override
    			public void onStopTrackingTouch(SeekBar seekBar)
    			{
    				// TODO Auto-generated method stub
    				//Toast.makeText(MainActivity.this, "结束华滑动", Toast.LENGTH_SHORT).show();
    			}
    			
    			@Override
    			public void onStartTrackingTouch(SeekBar seekBar)
    			{
    				// TODO Auto-generated method stub
    				//Toast.makeText(MainActivity.this, "開始滑动", Toast.LENGTH_SHORT).show();
    			}
    			
    			@Override
    			public void onProgressChanged(SeekBar seekBar, int progress,
    					boolean fromUser)
    			{
    				// TODO Auto-generated method stub
    				//Toast.makeText(MainActivity.this, "当前值是:"+progress, Toast.LENGTH_SHORT).show();
    				String str = String.format("%1$02x", progress);
    				editText1.setText(str); 
    				doWork();
    			}
    		});
            
            seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    		{
    			
    			@Override
    			public void onStopTrackingTouch(SeekBar seekBar)
    			{
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void onStartTrackingTouch(SeekBar seekBar)
    			{
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void onProgressChanged(SeekBar seekBar, int progress,
    					boolean fromUser)
    			{
    				// TODO Auto-generated method stub
    				//editText2.setText(Integer.toHexString(progress));
    				
    				editText2.setText(String.format("%1$02x", progress));
    				doWork();
    			}
    		});
            
            seekBar3.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    		{
    			
    			@Override
    			public void onStopTrackingTouch(SeekBar seekBar)
    			{
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void onStartTrackingTouch(SeekBar seekBar)
    			{
    				// TODO Auto-generated method stub
    				
    			}
    			
    			@Override
    			public void onProgressChanged(SeekBar seekBar, int progress,
    					boolean fromUser)
    			{
    				// TODO Auto-generated method stub
    				//editText3.setText(Integer.toHexString(progress));
    				editText3.setText(String.format("%1$02x", progress));
    				
    				doWork();
    			}
    		});
        }
    
        private void doWork()
    	{
        	String sTmp = editText1.getText().toString()+ editText2.getText().toString() + editText3.getText().toString();
    		textView.setText("当前值是:#" + sTmp);
    		TextView tView = (TextView)findViewById(R.id.textView5);
    		tView.setBackgroundColor(Color.parseColor("#"+sTmp));
    		
    		//tView.setBackgroundColor(Color.parseColor("#ff00"));
    	}
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }
    


  • 相关阅读:
    如何制作挖空的填空题试卷?
    原型制图工具有哪些?
    书籍推荐?来几本吧
    离线部署ELK+kafka日志管理系统【转】
    Elasticsearch5.0 安装问题集锦【转】
    在Nginx服务器上屏蔽IP
    MySQL Warning: Using a password on the command line interface can be insecure.解决办法
    不老的神器:安全扫描器Nmap渗透使用指南【转】
    MySQL数据库设置为只读及测试【转】
    Linux中切换用户变成-bash4.1-$的解决方法
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4602788.html
Copyright © 2011-2022 走看看