2017-12-26 14 views
0

私は現在、hereにあるSeekBarの例を使って作業しています。この例にはシークバーが1つありますが、もう2つのシークバーを追加するとどうなるか見たいと思っていました。トーストで変数名を印刷

リスナーはすべて動作しているように見えますが、タッチバーを追跡中に表示されるトーストにSeekBarの名前を追加します。しかし、私はトーストに変数の名前を追加するだけでは(seekBarに渡されたウィジェットは)、toString()の関数呼び出しに適合しません。

トーストに注目しているseekBarの名前を追加するにはどうすればよいですか?

MainActivity.java:

package com.javatpoint.seekbar; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.Toast; 
public class MainActivity extends Activity implements OnSeekBarChangeListener{ 
    SeekBar seekBar1, seekBar2, seekBar3; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     seekBar1=(SeekBar)findViewById(R.id.seekBar1); 
     seekBar1.setOnSeekBarChangeListener(this); 
     seekBar2=(SeekBar)findViewById(R.id.seekBar2); 
     seekBar2.setOnSeekBarChangeListener(this); 
     seekBar3=(SeekBar)findViewById(R.id.seekBar3); 
     seekBar3.setOnSeekBarChangeListener(this); 
} 
@Override 
public void onProgressChanged(SeekBar seekBar, int progress, 
     boolean fromUser) { 
    //This is where I want to print out the variable name 
    Toast.makeText(getApplicationContext(), "seekbar progress: "+progress, Toast.LENGTH_SHORT).show(); 
} 
@Override 
public void onStartTrackingTouch(SeekBar seekBar) { 
    Toast.makeText(getApplicationContext(),seekBar +"seekbar touch started!", Toast.LENGTH_SHORT).show(); 
} 
@Override 
public void onStopTrackingTouch(SeekBar seekBar) { 
    Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<SeekBar 
    android:id="@+id/seekBar1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="120dp" /> 
<SeekBar 
    android:id="@+id/seekBar2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    android:layout_marginTop="50dp" 
    android:layout_below="@id/seekBar1"/> 
<SeekBar 
    android:id="@+id/seekBar3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    android:layout_marginTop="50dp" 
    android:layout_below="@id/seekBar2"/> 

+0

ノートの最終バージョンでした。 –

答えて

0

誰かが私の問題を解決するためのタグの使用を詳述this post昨夜へのリンクを掲載activity_main.xmlが、残念ながら、彼らは自分の投稿を削除したか、削除されて正解とマークすることができました。

私はそのポストの情報を使って、各seekBarのタグとして文字列を設定し、タグを取得した新しい関数にシークバーを渡して、シークバーからの進捗状況を表示することができました各シークバー。 ここMainActivity

package com.javatpoint.seekbar; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.Toast; 
public class MainActivity extends Activity implements OnSeekBarChangeListener{ 
SeekBar seekBar1, seekBar2, seekBar3; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    seekBar1=(SeekBar)findViewById(R.id.seekBar1); 
    seekBar1.setOnSeekBarChangeListener(this); 
    seekBar1.setTag("1"); 
    seekBar2=(SeekBar)findViewById(R.id.seekBar2); 
    seekBar2.setOnSeekBarChangeListener(this); 
    seekBar2.setTag("2"); 
    seekBar3=(SeekBar)findViewById(R.id.seekBar3); 
    seekBar3.setOnSeekBarChangeListener(this); 
    seekBar3.setTag("3"); 
} 
@Override 
public void onProgressChanged(SeekBar seekBar, int progress, 
     boolean fromUser) { 
    changeValue(seekBar.getTag(), progress); 
} 
@Override 
public void onStartTrackingTouch(SeekBar seekBar) { 
    Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show(); 
} 
@Override 
public void onStopTrackingTouch(SeekBar seekBar) { 
    Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
public void changeValue(Object a, int progress){ 
    if(a=="1"){ 
     Toast.makeText(getApplicationContext(), "seekbar1 progress: "+progress, Toast.LENGTH_SHORT).show(); 
    } 
    if(a=="2"){ 
     Toast.makeText(getApplicationContext(), "seekbar2 progress: "+progress, Toast.LENGTH_SHORT).show(); 
    } 
    if(a=="3"){ 
     Toast.makeText(getApplicationContext(), "seekbar3 progress: "+progress, Toast.LENGTH_SHORT).show(); 
    } 
} 

}変数名は、典型的には、コンパイル時にのみ利用可能であること