1、界面布局xml代码:

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" >

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="当前值是:#000000" />

android:id="@+id/textView5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="20px"

android:width="100px"

android:text="test" />

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="红:" />

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" />

android:id="@+id/editText1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="0"

android:ems="10" >

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="绿" />

android:id="@+id/seekBar2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:max="255"

android:progress="0"

android:layout_weight="1" />

android:id="@+id/editText2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="00"

android:ems="10" />

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@+id/textView4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="蓝" />

android:id="@+id/seekBar3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:max="255"

android:progress="0"

android:layout_weight="1" />

android:id="@+id/editText3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="00"

android:ems="10" />

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;

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐