在Toast

时间:2017-12-26 21:18:33

标签: java android

我目前正在通过位于here的SeekBar示例工作。这个例子有一个搜索栏,但我想知道如果我再添加两个搜索栏会发生什么。

虽然听众似乎仍在工作,但我想将SeekBar的名称添加到跟踪触摸条时显示的吐司。但是,我不能只是将变量的名称添加到toast中(传入的seekBar是一个小部件),这不适合toString()的函数调用。

我如何添加我专注于Toast的搜索栏的名称?

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

activity_main.xml中

<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"/>

1 个答案:

答案 0 :(得分:0)

有人在昨晚发布了一个指向this post的链接,详细说明了使用标签来解决我的问题,但不幸的是,他们要么删除了帖子,要么删除了之后才能将其标记为正确的答案。

我能够使用该帖子中的信息将字符串设置为每个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();
    }
}

}