1

我阅读了一个如何向edittext显示搜索栏进度的示例,这是网页:

Android中的简单搜索栏

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

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

主要的:

<?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));
        }
        });

       }
      }
4

1 回答 1

2

使用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) {}
    }
});
于 2013-05-14T19:30:04.710 回答