Android:
Schieberegler seekbar
Mit einem SeekBar bekommt man in Android einen Schieberegler,
der Werte von 0 bis 100 einstellt.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textViewOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output
value"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:height="40sp"
android:width="200sp"
android:textSize="22sp"
/>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_alignParentBottom="true"
android:elevation="5sp" />
</RelativeLayout>
|
MainActivity.java
package com.example.demo_light;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends
AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textViewOutput =findViewById(R.id.textViewOutput);
SeekBar seekBar=findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int seekBarValue=0;
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
seekBarValue =i;
textViewOutput.setText("Seekbar="
+ i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "Seek bar progress is :" + seekBarValue,
Toast.LENGTH_SHORT).show();
textViewOutput.setText("Seekbar="
+ seekBarValue);
}
});
}
}
|