我读了一个示例,该示例显示如何在此网页的edittext中显示搜索栏的进度:

Simple Seekbar In Android

现在我的问题是,如果我在edittext框中引入一个数字,如何更改搜索栏?谢谢你的帮助

如果您可以访问该网页,我还将发布代码:

主要:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="94dp" >

    <requestFocus />
</EditText>

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


和Java代码:

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


public class SeekbarActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SeekBar sb=(SeekBar) findViewById(R.id.seekBar1);
    final EditText et=(EditText) findViewById(R.id.editText1);

    sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar)
        {
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar)
        {
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser)
        {
        //---change the font size of the EditText---

        et.setText(String.valueOf(progress));
        }
        });

       }
      }

最佳答案

使用addTextChangedListener

一个简单的例子:

//et and sk are class variables
et=(EditText)findViewById(R.id.editText);
sk = (SeekBar)findViewById(R.id.seekBar);

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        try{
            //Update Seekbar value after entering a number
            sk.setProgress(Integer.parseInt(s.toString()));
        } catch(Exception ex) {}
    }
});

关于java - 如何使用edittext更新搜索栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16550872/

10-16 17:06