简体   繁体   中英

Seekbars and EditTexts

I am working on an application in which I need the user to set an array of 30 values and to make it user friendly and easy to use I have decided to use a series of SeekBars and EditText widgets. I am using eclipse and have used it to set up the layout.

I am looking for an efficient way to set up the application to automatically update the value of the SeekBar when the value of the EditText has been changed and then use that same value in a application integer array.

<SeekBar android:layout_height="wrap_content" android:layout_width="wrap_content" android:max="255" android:layout_gravity="center_vertical" android:layout_weight="1" android:padding="10dp" android:id="@+id/seekX"></SeekBar> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight=".01" android:minWidth="50sp" android:maxWidth="50sp" android:gravity="center_horizontal" android:padding="10dp" android:layout_marginRight="10dp" android:inputType="number" android:id="@+id/editX"></EditText>

The seek bars and EditText fields are named in a corresponding way editX should match seekX as editY should match seekY and so on. I am also storing the values in the int[] upgradeValues.

I have also set the SeekBar to have a max of 255 but i need a way to automatically change any value set in the EditText field above 255 down to 255 and similarly any value below 0 to 0.

Basically I need an efficient way of making "(ValueOF)seekX = (ValueOf)editX = (ValueOf)upgradeValues[x] >= 0 <= 255" and be updated if seekX or editX is changed.

I am still pretty new to Android development, so this answer isn't very specific, but hopefully it gets you on your way.

There are a few approaches. You can set change listeners on both of your seekbar and the edittext that update the values of the other (suppose you have your EditText t, SeekBar b and the value in model:

b.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        @Override
        public void onProgressChanged(final SeekBar seekBar,
            final int progress,
            final boolean fromUser)
        {
            if (fromUser)
            {
                model = progress;
                updateUI();
            }
        }

        @Override
        public void onStartTrackingTouch(final SeekBar seekBar)
        {

        }

        @Override
        public void onStopTrackingTouch(final SeekBar seekBar)
        {

        }
    });
    t.setOnKeyListener(new View.OnKeyListener()
    {
        @Override
        public boolean onKey(final View v,
            final int keyCode,
            final KeyEvent event)
        {
            model = Integer.parseInt(t.getText().toString());
            updateUI();
            return true;
        }
    });

Then updateUI() would be something like:

private void updateUI()
{
    t.setText(model);
    b.setProgress(model);
}

Those are some crazy names and I clearly did no error checking. But you get the idea. This is probably NOT the way you want to go, because you have to make sure you catch all the different ways the user interacts with each component (onKey probably isn't enough to detect whenever the EditText value changes).

The proper (MVC) way is to have your model (your int[]) contain the value and have your SeekBar and EditText use your model as their models. I don't know how to do that in Android's UI model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM