Android

2013年2月2日 星期六

Android:SeekBar


1.Main.java
package wei.finalex;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

  SeekBar sbr,sbg,sbb;
    LinearLayout linear;
    TextView tvr,tvg,tvb;
    int red,green,blue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviews();
}

void findviews(){

sbr=(SeekBar)findViewById(R.id.sb1);
sbg=(SeekBar)findViewById(R.id.sb2);
sbb=(SeekBar)findViewById(R.id.sb3);
linear=(LinearLayout)findViewById(R.id.LinearLayout1);
tvr=(TextView)findViewById(R.id.tv1);
tvg=(TextView)findViewById(R.id.tv2);
tvb=(TextView)findViewById(R.id.tv3);
sbr.setOnSeekBarChangeListener(sblistener);
sbg.setOnSeekBarChangeListener(sblistener);
sbb.setOnSeekBarChangeListener(sblistener);
}


OnSeekBarChangeListener sblistener=new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

Toast.makeText(MainActivity.this," Red: "+red+" Green: "+green+" Blue: "+blue,Toast.LENGTH_SHORT).show();

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
   tvr.setBackgroundColor(Color.WHITE);
   tvg.setBackgroundColor(Color.WHITE);
   tvb.setBackgroundColor(Color.WHITE);
if(seekBar==sbr){
red=progress;
linear.setBackgroundColor(Color.rgb(red,green, blue));

}else if(seekBar==sbg){
green=progress;
linear.setBackgroundColor(Color.rgb(red,green,blue));

}else {
blue=progress;
linear.setBackgroundColor(Color.rgb(red,green,blue));

}


}
};



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}
------------------------------------------------------------------------------------------------------------

2.activity_main.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" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv1"
            android:textColor="#F00"/>

        <SeekBar
            android:id="@+id/sb1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"  />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv2"
            android:textColor="#0F0" />

        <SeekBar
            android:id="@+id/sb2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv3"
            android:textColor="#00F"/>

        <SeekBar
            android:id="@+id/sb3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255" />

    </TableRow>

</LinearLayout>

沒有留言:

張貼留言