2013-05-14 21 views
0

我讀了如何搜索條的進步黨展現給EditText上這樣的一個例子,以更新搜索條的網頁:如何使用的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)); 
     } 
     }); 

     } 
     } 

回答

1

使用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) {} 
    } 
}); 
+0

它的工作就像一個魅力感謝您的幫助 – JUAN

+0

這不適合我不幸的。你能幫我嗎? http://stackoverflow.com/questions/20802397/change-seekbar-progress-based-on-edittext-value – Si8