如何用代码制作SeekBar接口?
How to make SeekBar interface with code?
我在片段中创建了几个 SeekBar,在 java 文件中创建了一个 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布局
<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 代码通信,尽管我很确定我已经正确地完成了所有操作。如果有人能看到我在这里犯的任何严重错误,将不胜感激。
这是您的查询的解决方案:
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;
}
谢谢,编码愉快。
我在片段中创建了几个 SeekBar,在 java 文件中创建了一个 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布局
<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 代码通信,尽管我很确定我已经正确地完成了所有操作。如果有人能看到我在这里犯的任何严重错误,将不胜感激。
这是您的查询的解决方案:
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;
}
谢谢,编码愉快。