Androidの編集テキストに電話番号を入力するときに、(xxx)xxx-xxxxxの形式で電話番号を設定するにはどうすればよいですか?
(,),- 文字が特定の位置に自動的に追加されます。
コードを書きましたが、andorid 2.2 バージョンでのみ機能し、上記のバージョンでは機能しません。 stackoverflow でさらに多くの質問を検索しましたが、取得できませんでした。
私のコードをチェックしてください:
phone.addTextChangedListener(new TextWatcher () {
@Override
public void afterTextChanged(Editable chars) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(final CharSequence s, final int start, int lengthBefore,
int lengthAfter) {
// TODO Auto-generated method stub
count = start;
/*if (phone.getText().length() <= 0) {
phone.append("(");
phone.setSelection(1);
}else if (count == 3) {
phone.append(")");
}else if (count == 7) {
phone.append("-");
}
*/
}
});
phone.setOnKeyListener(new OnKeyListener (){
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg1 == KeyEvent.KEYCODE_DEL){
if(count > 0){
count = count - 1;
}
}else{
if(phone.getText().length() <= 0){
phone.append("(");
}else{
if(count == 3){
phone.append(")");
}else if(count == 7){
phone.append("-");
}
}
}
return false;
}
});
}
次のように表示されるログキャット:
01-03 15:07:23.529: W/InputManagerService(153): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40900f88
01-03 15:07:23.529: D/StatusBarPolicy(221): mFullChargeListener
01-03 15:07:25.749: V/AudioPolicyManager(84): startOutput() output 1, stream 1, session 1488
01-03 15:07:25.749: V/AudioPolicyManager(84): changeRefCount() stream 1, count 1
01-03 15:07:25.749: V/AudioPolicyManager(84): getDeviceForStrategy() from cache strategy 0, device 2
01-03 15:07:25.749: V/AudioPolicyManager(84): getNewDevice() selected device 2
01-03 15:07:25.749: V/AudioPolicyManager(84): setOutputDevice() output 1 device 2 delayMs 0
01-03 15:07:25.749: V/AudioPolicyManager(84): setOutputDevice() setting same device 2 or null device for output 1
01-03 15:07:25.749: V/AudioPolicyManager(84): releaseOutput() 1
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): open driver
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): get config
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): set config
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): buffer_size: 4800
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): buffer_count: 2
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): channel_count: 2
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): sample_rate: 44100
01-03 15:07:25.939: W/AudioFlinger(84): write blocked for 164 msecs, 61 delayed writes, thread 0xcdd0
01-03 15:07:26.009: V/AudioPolicyManager(84): stopOutput() output 1, stream 1, session 1488
01-03 15:07:26.009: V/AudioPolicyManager(84): changeRefCount() stream 1, count 0
01-03 15:07:26.009: V/AudioPolicyManager(84): getNewDevice() selected device 0
01-03 15:07:26.009: V/AudioPolicyManager(84): setOutputDevice() output 1 device 0 delayMs 0
01-03 15:07:26.009: V/AudioPolicyManager(84): setOutputDevice() setting same device 0 or null device for output 1
01-03 15:07:26.699: V/AudioPolicyManager(84): startOutput() output 1, stream 1, session 1489
01-03 15:07:26.699: V/AudioPolicyManager(84): changeRefCount() stream 1, count 1
01-03 15:07:26.699: V/AudioPolicyManager(84): getDeviceForStrategy() from cache strategy 0, device 2
01-03 15:07:26.699: V/AudioPolicyManager(84): getNewDevice() selected device 2
01-03 15:07:26.699: V/AudioPolicyManager(84): setOutputDevice() output 1 device 2 delayMs 0
01-03 15:07:26.699: V/AudioPolicyManager(84): setOutputDevice() setting same device 2 or null device for output 1
01-03 15:07:26.699: V/AudioPolicyManager(84): releaseOutput() 1
01-03 15:07:26.859: V/AudioPolicyManager(84): stopOutput() output 1, stream 1, session 1489
01-03 15:07:26.859: V/AudioPolicyManager(84): changeRefCount() stream 1, count 0
01-03 15:07:26.859: V/AudioPolicyManager(84): getNewDevice() selected device 0
01-03 15:07:26.859: V/AudioPolicyManager(84): setOutputDevice() output 1 device 0 delayMs 0
01-03 15:07:26.859: V/AudioPolicyManager(84): setOutputDevice() setting same device 0 or null device for output 1
01-03 15:07:27.499: I/StatusBarPolicy(221): BAT. status:5 health:2