0

我正在尝试在单个活动中实现多个搜索栏

过滤器.java

public class Filters extends Activity implements OnSeekBarChangeListener{
    private SeekBar PRICEbar,DISTANCEbar, RATINGbar; // declare seekbar object variable
    // declare text label objects
    private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.filters);     
        PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
        PRICEbar.setOnSeekBarChangeListener(this); // set seekbar listener.
        // since we are using this class as the listener the class is "this"

        // make text label for progress value
        PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
        // make text label for action
        //textAction = (TextView)findViewById(R.id.textViewAction);


        ////////////////////////////////////////////////////////
        DISTANCEbar = (SeekBar)findViewById(R.id.DISTANCEseekBarID); // make seekbar object
        DISTANCEbar.setOnSeekBarChangeListener(this);
        DISTANCEtextProgress = (TextView)findViewById(R.id.DISTANCEtextViewProgressID);

        ////////////////////////////////////////////////////////
        RATINGbar = (SeekBar)findViewById(R.id.RATINGseekBarID); // make seekbar object
        RATINGbar.setOnSeekBarChangeListener(this);
        RATINGtextProgress = (TextView)findViewById(R.id.RATINGtextViewProgressID);



    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub

        // change progress text label with current seekbar value
        PRICEtextProgress.setText("Price:: Rs "+progress);
        // change action text label to changing
        //textAction.setText("changing");

        DISTANCEtextProgress.setText("Distance:: "+progress);

        RATINGtextProgress.setText("Rating:: "+progress);



    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        //textAction.setText("starting to track touch");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        seekBar.setSecondaryProgress(seekBar.getProgress());
        //textAction.setText("ended tracking touch");       
    }

}

过滤器.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayoutforPRICE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/PRICEtextViewProgressID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Price"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

        <SeekBar
            android:id="@+id/PRICEseekBarID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="26dp"
            android:max="100" >
        </SeekBar>
    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

    <RelativeLayout
        android:id="@+id/relativeLayoutforDISTANCE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/DISTANCEtextViewProgressID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="DISTANCE"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

        <SeekBar
            android:id="@+id/DISTANCEseekBarID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="26dp"
            android:max="100" >
        </SeekBar>
    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />

    <RelativeLayout
        android:id="@+id/relativeLayoutforRATING"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/RATINGtextViewProgressID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Rating"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

        <SeekBar
            android:id="@+id/RATINGseekBarID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="26dp"
            android:max="100" >
        </SeekBar>
    </RelativeLayout>

</LinearLayout>

在此处输入图像描述


  • 我们可以清楚地知道,无论哪个搜索栏,我都会立即更改所有更改的值

  • 我该如何解决这个问题?

  • 另外,我怎样才能让评级搜索栏接受像( 1,2,3,4,5 ) 而不是(1234..100)

有任何想法吗 ?

4

1 回答 1

1

当它们的值发生变化时,您所有的 SeekBar 都会在同一个对象 (this) 上调用相同的方法。你可以做两件事:

  1. 在您的处理程序方法中,检查哪个 SeekBar 触发了调用。

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar == PRICEbar)
            PRICEtextProgress.setText("Price:: Rs "+progress);
        else if (seekBar == DISTANCEbar)
            DISTANCEtextProgress.setText("Distance:: "+progress);
        else if (seekBar == RATINGbar)
            RATINGtextProgress.setText("Rating:: "+progress);
    }
    
  2. 您也可以只使用内联匿名类

    public class Filters extends Activity {  //Remove implements OnSeekBarChangeListener
    
    ...
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.filters);
    
        // make text label for progress value
        PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
        PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
        PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                PRICEtextProgress.setText("Price:: Rs "+progress);
            }
        });
    
    
        ////////////////////////////////////////////////////////
        DISTANCEtextProgress = (TextView)findViewById(R.id.DISTANCEtextViewProgressID);
        DISTANCEbar = (SeekBar)findViewById(R.id.DISTANCEseekBarID); // make seekbar object
        DISTANCEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                DISTANCEtextProgress.setText("Distance:: "+progress);
            }
        });
    
    
    
        ////////////////////////////////////////////////////////
        RATINGtextProgress = (TextView)findViewById(R.id.RATINGtextViewProgressID);
        RATINGbar = (SeekBar)findViewById(R.id.RATINGseekBarID); // make seekbar object
        RATINGbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener {
            @Override
            public void onProgressChanged(
                SeekBar seekBar, int progress, boolean fromUser) {
                RATINGtextProgress.setText("Rating:: "+progress);
            }
        });
    }
    

最后,您可以使用 seekBar.setMax(int) 将搜索栏的最大值限制为 5。

于 2013-10-05T07:03:50.647 回答