Ich habe ein paar SeekBars in einem Fragment und einem OnSeekBarChangeListener in meiner Java-Datei erstellt, aber es scheint eine ungeklärte Trennung zwischen dem XML-Layout und der Java-Datei zu geben.Wie man SeekBar Schnittstelle mit Code bildet?
Java-Klasse
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_preferences, container, false);
final SeekBar s1 = (SeekBar) root.findViewById(R.id.s1Bar);
final TextView spicinessText = (TextView) root.findViewById(R.id.spicinessTxt);
s1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Toast.makeText(getContext(), String.valueOf(i), Toast.LENGTH_SHORT).show();
spicinessText.setText("Spiciness " + String.valueOf(i));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return inflater.inflate(R.layout.fragment_preferences, container, false);
}
xml Layout
<FrameLayout>
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spiciness"
android:id="@+id/spicinessTxt"/>
<SeekBar
android:id="@+id/s1Bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sourness"/>
<SeekBar
android:id="@+id/seekBar4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sweet"
android:id="@+id/sweet"/>
<SeekBar
android:id="@+id/seekBar5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Salty"/>
<SeekBar
android:id="@+id/seekBar6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
startete ich die App auf einem externen Gerät und die tatsächliche Layout einfach nicht mit dem Java kommunizieren Code, obwohl ich mir ziemlich sicher bin, dass ich alles richtig gemacht habe. Wenn irgendjemand irgendeinen kritischen Fehler sehen kann, den ich hier gemacht habe, würde er sehr geschätzt werden.
Haben Sie Fragment auf Aktivität gestartet ?? –
Ich habe ein Fragment für die Java-Klasse verwendet. –
Hey @kyle kannst du bitte dein Projekt mit mir teilen? –