Using a simple seekbar in android

我是研究僧i 提交于 2019-12-24 15:23:42

问题


How to get the values from SeekBar and pass them as intents to another activity


I have three seekBars as below

  • PRICEbar
  • DISTANCEbar
  • RATINGbar

  1. I want to get the selected value from the three seek bars and pass them as intents to another activity through DoneIntent
  2. How to make this happen

Filters.java

public class Filters extends Activity implements OnSeekBarChangeListener{

    // declare text objects variables
    private SeekBar PRICEbar,DISTANCEbar, RATINGbar; 
    private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;

    Button back;
    Button Done;


    private RadioGroup rdg;
    private RadioButton indian;
    private RadioButton thai;
    private RadioButton chinese;


    private String selectedType="";



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.filters);   


        /** Finding all the views in this Activity. */
        back=(Button) findViewById(R.id.TopNavigationBarFilterBackButton);




        Done=(Button) findViewById(R.id.SEARCH_BUTTON_ID);
        Done.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent DoneIntent=new Intent(Filters.this,AndroidTabRestaurantDescFilterListView.class);

                DoneIntent.putExtra("REST1",selectedType);
                startActivity(DoneIntent);
            }
        });



        PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
        PRICEbar.setOnSeekBarChangeListener(this); 
        PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
                PRICEtextProgress.setText("Price:: Rs "+progress);
                seekBar.setMax(100);

            }
        });




}

回答1:


use PRICEbar.getProgress(); to get current value of your seekbar. and then pass them as intent with this code:

Intent intent = new Intent(MainActivity.this, YourNewActivity.class);
intent.PutExtra("PriceBar", PRICEbar.getProgress());
intent.PutExtra("DistanceBar", DISTANCEbar.getProgress());
intent.PutExtra("RatingBar", RATINGbar.getProgress());
startActivity(intent);


来源:https://stackoverflow.com/questions/19486877/using-a-simple-seekbar-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!