(values)strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resonrces>
<string name="app_name">Widget Demo 4<string>
<string name="ui_message">Change brightness with SeekBar<string>
<string name="menu_settings">Settings<string>
</resources>
(layout)activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android::orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:bacground="#EEF7C1"
android:text="@string/ui_message"/>
<SeekBar
android:id="@+id/sbColor"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:max="255"
android::progress="0" />
</LinearLayout>
MainActivity.java
package nn.widgetdemo4;
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.Toast;
public class MainActivity extends Activity {
private LinearLayout llBackground;
private SeekBar sbColor;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runDemo();
}
@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;
}
private void runDemo(){
llBackground=(LinearLayout) findViewById(R.id.llBackground);
sbColor=(SeekBar) findViewById(R.id.sbColor);
sbColor=setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
llBackground.setBackgroundColor(
Color.rgb(progress,progress,progress));
}
@Override
public void onStopTrackingTouch(SeekBar seekBar){
String msg="Red,Green,Blue value."+seekBar.getProgress();
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar){
//
}
})
}
}
沒有留言:
張貼留言