我在我的java文件中创建了一个片段中的一些SeekBars和一个OnSeekBarChangeListener,但是xml布局和java文件之间似乎存在无法解释的断开连接。
java class
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=""/>
我在外部设备上启动了应用程序,实际布局根本就没有与java代码进行通信,即使我非常确定我已经正确地完成了所有操作。如果有人能看到我在这里发生的任何重大错误,我将不胜感激。
答案 0 :(得分:0)
以下是您的查询的解决方案:
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 root;
}
谢谢你,编码愉快。