2017-12-02 107 views
0

我在一個片段中創建了一些SeekBars,在我的java文件中創建了一個OnSeekBarChangeListener,但是似乎在xml佈局和java文件之間存在不明原因的斷開。如何使用代碼製作SeekBar界面?

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 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通信代碼,即使我很確定我已經完成了所有的工作。如果任何人都可以看到我在這裏做出的任何嚴重錯誤,那將非常感激。

+0

您是否在活動上推出了片段? –

+0

我使用了java類的一個片段。 –

+0

嘿@kyle你能不能請你和我分享你的項目? –

回答

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; 
    } 

感謝和快樂編碼。