2010-09-02 69 views
0

我在Android的初學者超過,如何在性格特徵極限的EditText超過玩的嘟嘟聲,我已經通過設置addTextChangeListener嘗試,但它不會讓事情發生後超限所以着播放的聲音和onKeylistener只適用於物理密鑰,所以請給我任何提示以解決問題。蜂鳴聲時editext字LIMT在android系統

這是我的代碼

private EditText editTextBox; private MediaPlayer mMediaPlayer; private int len; private int limit = 10;

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    // setContentView(new EditText(this)); 

    setContentView(R.layout.main); 
    editTextBox = (EditText) findViewById(R.id.EditTextBox); 

    editTextBox.addTextChangedListener(new TextWatcher() { 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      if (s.length() > 10) { 
       playAudio(); 
      } 
      if (s.length() == limit) { 
       len = limit + 1; 
      } else { 
       len = s.length(); 
      } 
      Log.e("beep", "onTextChanged" + " " + count); 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      Log.e("beep", "beforeTextChanged"); 
     } 

     public void afterTextChanged(Editable s) { 
      Log.e("beep", "afterTextChanged"); 
     } 
    }); 
    editTextBox.setOnKeyListener(new OnKeyListener() { 

     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      Log.e("beep", "onKeyListener" + " " + len); 
      if (len == limit + 1) { 
       len++; 
      } else { 
       if (len > limit) { 
        if (!(keyCode == KeyEvent.KEYCODE_DEL)) 
         playAudio(); 
       } 
      } 
      Log.e("beep", "onKeyListener"); 
      return false; 
     } 
    }); 

} 

private void playAudio() { 
    try { 

     mMediaPlayer = MediaPlayer.create(this, R.drawable.beep1); 
     mMediaPlayer.setLooping(false); 
     Log.e("beep", "started0"); 
     mMediaPlayer.start(); 
     Log.e("beep", "started1"); 
     mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { 
      public void onCompletion(MediaPlayer arg0) { 
       // finish(); 
       stopAudio(); 
      } 
     }); 
    } catch (Exception e) { 
     Log.e("beep", "error: " + e.getMessage(), e); 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    stopAudio(); 
} 

private void stopAudio() { 
    if (mMediaPlayer != null) { 
     mMediaPlayer.release(); 
     mMediaPlayer = null; 
    } 
} 

在此先感謝

+0

您可以發佈您當前的代碼? – Wroclai 2010-09-02 08:18:31

+0

我發佈了代碼 – Navya 2010-09-02 09:04:20

回答

0

在我看來,像你的編碼到多得到聲音。會更容易些,實例化的聲音一次,不僅僅是使用此行代碼中所需要的條件,讓聲音

mMediaPlayer.start();