简体   繁体   English

实现2个Seekbars-两者都修改完全相同的文本?

[英]Implementing 2 Seekbars - Both Modify The Exact Same Text?

I'm attempting to implement a 2nd seekbar but for some odd reason they're both modifying the same text when the seekbars are moved. 我正在尝试实现第二个搜索栏,但是由于某些奇怪的原因,它们在移动搜索栏时都修改了相同的文本。 I've looked the source code up and down and can't identify the issue (I know I'm overlooking something very simple - can anyone spot it?) 我上下查看了源代码,无法确定问题所在(我知道我正在忽略一些非常简单的内容-有人可以发现吗?)

JAVA: JAVA:

     private long rowID; 
     private EditText nameEt;
     private EditText capEt;
     private EditText codeEt;
     private TimePicker timeEt;
     private TextView ssidTextView;
     private TextView sbTv;
     private SeekBar bar;
     private SeekBar bar2;
     private SeekBar bar;
     private SeekBar bar2;
     private TextView textProgress,textAction;
     private TextView textProgress2,textAction2;



       public void onCreate(Bundle savedInstanceState) 
       {
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.add_country);
          bar = (SeekBar)findViewById(R.id.seekBar1);
          bar.setOnSeekBarChangeListener(this);
          bar2 = (SeekBar)findViewById(R.id.seekBar2);
          bar2.setOnSeekBarChangeListener(this);



          textProgress = (TextView)findViewById(R.id.textViewProgress);
          textProgress2 = (TextView)findViewById(R.id.textViewProgress2);

          textAction = (TextView)findViewById(R.id.textViewAction);
          textAction2 = (TextView)findViewById(R.id.textViewAction2);

      }

       @Override
       public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {     

        textProgress.setText(progress+"MBPS");
       }

        public void onProgressChanged2(SeekBar seekBar2, int progress,
                boolean fromUser) {


        textProgress2.setText(progress+"MBPS");

The only function being called here is the first one: 在这里调用的唯一函数是第一个:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {    
    textProgress.setText(progress+"MBPS");
}

In this function, you should check which SeekBar is passed and react accordingly: 在此函数中,您应该检查通过了哪个SeekBar并做出相应的反应:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {     
    if (seekBar.equals(bar)) {//do something for bar}
    else if (seekBar.equals(bar2)) {//do something for bar2}
}

If your layout is defined in XML (which you should do anyways in a simple app like this), then compare the SeekBars ids: 如果您的布局是用XML定义的(无论如何,您都应该在这样的简单应用中执行此操作),然后比较SeekBars ID:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {     
    if (seekBar.getId() == R.id.seekbar_1) {//do something for bar}
    else if (seekBar.getId() == R.id.seekbar_2) {//do something for bar2}
}

Here is a complete solution 这是一个完整的解决方案

Activity 活动

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{

    SeekBar bar1;
    SeekBar bar2;
 TextView textProgress1,textProgress2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bar1=(SeekBar)findViewById(R.id.seekBar1);
            bar2=(SeekBar)findViewById(R.id.seekBar2);
            bar1.setOnSeekBarChangeListener(this);
            bar2.setOnSeekBarChangeListener(this);
            textProgress1=(TextView)findViewById(R.id.textView1);
            textProgress2=(TextView)findViewById(R.id.textView2);
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            switch (seekBar.getId()) {
            case R.id.seekBar1:
                textProgress1.setText(progress +" MBPS");
                break;
          case R.id.seekBar2:
              textProgress2.setText(progress +" MBPS");
                break;
            default:
                break;
            }
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
 }

activity_main Xml activity_main Xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

<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="39dp" 
    />

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/seekBar1"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="38dp"
    android:text="TextView Progrss 1" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/seekBar1"
    android:layout_marginTop="19dp"
    android:text="TextView Progrss 2"  />

</RelativeLayout>

What is wrong in your code is that the method that is called by the seekbar listener is the one with @override notation above it. 代码中的问题是,seekbar侦听器调用的方法是上面带有@override表示法的方法。

My solution will change the text when the seek is complete , if you need real time change for eg: changing text as soon as it starts the seek, 我的解决方案将在搜索完成后更改文本,如果您需要实时更改,例如:开始搜索后立即更改文本,

Implement the following method................... 实现以下方法...

[ http://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html#onProgressChanged(android.widget.SeekBar , int, boolean)][1] and use the same logic specified below... [ http://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html#onProgressChanged(android.widget.SeekBar,int,boolean )] [1],并使用下面指定的相同逻辑...

Implement the 实施

@override
public void onStopTrackingTouch(SeekBar s){
   switch (arg0.getId()) {
    case R.id.seekBar1:
        setText("ONE");
                    break;
    case R.id.seekBar2:
        setText("TWO");
                    break;  
    default:
        break;        
    }

} }

I hope this solves the issue. 我希望这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一活动中使用2个搜索栏 - using 2 seekbars in the same activity 在java泛型实现接口上强制完全相同的类型 - force exact same type on java generics implementing interface 如何在文本视图中求和两个搜索栏的进度值? - How to sum progress values of two seekbars in text view? 如何根据solr中的运算符在同一字段上执行精确搜索和词干搜索 - How to perform both exact search and stemmed search on the same field depending upon the operator in solr 实现两个都声明相同方法的接口时出现意外的编译器错误-(一个抽象,一个默认) - Unexpected compiler error when implementing 2 interfaces which both declare the same method - (one abstract and one default) 同时在jframe中显示标签,即文本等和形状 - Displaying both labels ie text etc and shapes in jframe at same time Splitpane JavaFX:在两个窗格上以相同级别呈现文本 - Splitpane JavaFX: Text rendering on same level on both panes Jtable在同一行中左右对齐文本 - Jtable align text both on left and right in the same line 如何以最简单的方式在同一JSP页面中同时显示文本和图像? - How to display Text and images both in same JSP page in easiest way? 阅读和分析同一行中同时包含数字和字母的文本文件 - Read and analyze text files containing both numbers and letters on the same line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM