2010年10月6日水曜日

Android Filterを使ってみた

android.widget.Filter を使ってみた。

例えば、EditText で英数字しか入力できないようにするには
こんな感じにする。


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

EditText ed = (EditText)findViewById(R.id.edittext);
ed.setFilters(filters);
}

private InputFilter[] filters = { new MyFilter() };

class MyFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {

if( source.toString().matches("^[a-zA-Z0-9]+$") ){
return source;
}else{
return "";
}
}
}
}


filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend)
の各引数について

source: 今入力されていて、確定していない文字列
     つまり、一般的に下線がでている変換前の文字列

start : 今入力されていて、確定していない文字列の先頭位置
     常に 0 になる(みたいだ)

end  : 今入力されていて、確定していない文字列の終端位置
     常に確定していない文字列の長さになる(みたいだ)

dest : 今EditTextに入っている文字列、確定していない文字列も含む

dstart : 今入力されていて確定していない文字列の、 EditText
     に入っている文字列上での先頭位置
     
dend : 今入力されていて確定していない文字列の、 EditText に
    入っている文字列上での終端位置

試しにログを出してみた。


--- a ---

D/filterTest(14321): source : a
D/filterTest(14321): start : 0
D/filterTest(14321): end : 1
D/filterTest(14321): dest :
D/filterTest(14321): dstart : 0
D/filterTest(14321): dend : 0
D/filterTest(14321): source : a
D/filterTest(14321): start : 0
D/filterTest(14321): end : 1
D/filterTest(14321): dest : a
D/filterTest(14321): dstart : 0
D/filterTest(14321): dend : 1

--- b ---

D/filterTest(14321): source : ab
D/filterTest(14321): start : 0
D/filterTest(14321): end : 2
D/filterTest(14321): dest : a
D/filterTest(14321): dstart : 0
D/filterTest(14321): dend : 1
D/filterTest(14321): source : ab
D/filterTest(14321): start : 0
D/filterTest(14321): end : 2
D/filterTest(14321): dest : ab
D/filterTest(14321): dstart : 0
D/filterTest(14321): dend : 2

--- c ---

D/filterTest(14321): source : abc
D/filterTest(14321): start : 0
D/filterTest(14321): end : 3
D/filterTest(14321): dest : ab
D/filterTest(14321): dstart : 0
D/filterTest(14321): dend : 2
D/filterTest(14321): source : abc
D/filterTest(14321): start : 0
D/filterTest(14321): end : 3
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 0
D/filterTest(14321): dend : 3

--- Enter ---

D/filterTest(14321): source : abc
D/filterTest(14321): start : 0
D/filterTest(14321): end : 3
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 0
D/filterTest(14321): dend : 3
D/filterTest(14321): source :
D/filterTest(14321): start : 0
D/filterTest(14321): end : 1
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 3
D/filterTest(14321): dend : 3

--- あ ---

D/filterTest(14321): source : あ
D/filterTest(14321): start : 0
D/filterTest(14321): end : 1
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 3
D/filterTest(14321): dend : 3

--- い ---

D/filterTest(14321): source : あい
D/filterTest(14321): start : 0
D/filterTest(14321): end : 2
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 3
D/filterTest(14321): dend : 3

--- う ---

D/filterTest(14321): source : あいう
D/filterTest(14321): start : 0
D/filterTest(14321): end : 3
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 3
D/filterTest(14321): dend : 3

--- Enter ---

D/filterTest(14321): source : あいう
D/filterTest(14321): start : 0
D/filterTest(14321): end : 3
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 3
D/filterTest(14321): dend : 3
D/filterTest(14321): source :
D/filterTest(14321): start : 0
D/filterTest(14321): end : 0
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 3
D/filterTest(14321): dend : 3

--- 1 ---

D/filterTest(14321): source : 1
D/filterTest(14321): start : 0
D/filterTest(14321): end : 1
D/filterTest(14321): dest : abc
D/filterTest(14321): dstart : 3
D/filterTest(14321): dend : 3

--- 2 ---

D/filterTest(14321): source : 2
D/filterTest(14321): start : 0
D/filterTest(14321): end : 1
D/filterTest(14321): dest : abc1
D/filterTest(14321): dstart : 4
D/filterTest(14321): dend : 4

--- 3 ---

D/filterTest(14321): source : 3
D/filterTest(14321): start : 0
D/filterTest(14321): end : 1
D/filterTest(14321): dest : abc12
D/filterTest(14321): dstart : 5
D/filterTest(14321): dend : 5



 

0 件のコメント:

コメントを投稿

'},ClipboardSwf:null,Version:'1.5.1'}};dp.SyntaxHighlighter=dp.sh;dp.sh.Toolbar.Commands={ExpandSource:{label:'+ expand source',check:function(highlighter){return highlighter.collapse;},func:function(sender,highlighter) {sender.parentNode.removeChild(sender);highlighter.div.className=highlighter.div.className.replace('collapsed','');}},ViewSource:{label:'view plain',func:function(sender,highlighter) {var code=dp.sh.Utils.FixForBlogger(highlighter.originalCode).replace(/'+code+'');wnd.document.close();}},CopyToClipboard:{label:'copy to clipboard',check:function(){return window.clipboardData!=null||dp.sh.ClipboardSwf!=null;},func:function(sender,highlighter) {var code=dp.sh.Utils.FixForBlogger(highlighter.originalCode).replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');if(window.clipboardData) {window.clipboardData.setData('text',code);} else if(dp.sh.ClipboardSwf!=null) {var flashcopier=highlighter.flashCopier;if(flashcopier==null) {flashcopier=document.createElement('div');highlighter.flashCopier=flashcopier;highlighter.div.appendChild(flashcopier);} flashcopier.innerHTML='';} alert('The code is in your clipboard now');}},PrintSource:{label:'print',func:function(sender,highlighter) {var iframe=document.createElement('IFRAME');var doc=null;iframe.style.cssText='position:absolute;width:0px;height:0px;left:-500px;top:-500px;';document.body.appendChild(iframe);doc=iframe.contentWindow.document;dp.sh.Utils.CopyStyles(doc,window.document);doc.write('

'+highlighter.div.innerHTML+'

');doc.close();iframe.contentWindow.focus();iframe.contentWindow.print();alert('Printing...');document.body.removeChild(iframe);}},About:{label:'?',func:function(highlighter) {var wnd=window.open('','_blank','dialog,width=300,height=150,scrollbars=0');var doc=wnd.document;dp.sh.Utils.CopyStyles(doc,window.document);doc.write(dp.sh.Strings.AboutDialog.replace('{V}',dp.sh.Version));doc.close();wnd.focus();}}};dp.sh.Toolbar.Create=function(highlighter) {var div=document.createElement('DIV');div.className='tools';for(var name in dp.sh.Toolbar.Commands) {var cmd=dp.sh.Toolbar.Commands[name];if(cmd.check!=null&&!cmd.check(highlighter)) continue;div.innerHTML+=''+cmd.label+'';} return div;} dp.sh.Toolbar.Command=function(name,sender) {var n=sender;while(n!=null&&n.className.indexOf('dp-highlighter')==-1) n=n.parentNode;if(n!=null) dp.sh.Toolbar.Commands[name].func(sender,n.highlighter);} dp.sh.Utils.CopyStyles=function(destDoc,sourceDoc) {var links=sourceDoc.getElementsByTagName('link');for(var i=0;i');} dp.sh.Utils.FixForBlogger=function(str) {return(dp.sh.isBloggerMode==true)?str.replace(/
|<br\s*\/?>/gi,''):str;} dp.sh.RegexLib={MultiLineCComments:new RegExp('/\\*[\\s\\S]*?\\*/','gm'),SingleLineCComments:new RegExp('//.*$','gm'),SingleLinePerlComments:new RegExp('#.*$','gm'),DoubleQuotedString:new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),SingleQuotedString:new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'",'g')};dp.sh.Match=function(value,index,css) {this.value=value;this.index=index;this.length=value.length;this.css=css;} dp.sh.Highlighter=function() {this.noGutter=false;this.addControls=true;this.collapse=false;this.tabsToSpaces=true;this.wrapColumn=80;this.showColumns=true;} dp.sh.Highlighter.SortCallback=function(m1,m2) {if(m1.indexm2.index) return 1;else {if(m1.lengthm2.length) return 1;} return 0;} dp.sh.Highlighter.prototype.CreateElement=function(name) {var result=document.createElement(name);result.highlighter=this;return result;} dp.sh.Highlighter.prototype.GetMatches=function(regex,css) {var index=0;var match=null;while((match=regex.exec(this.code))!=null) this.matches[this.matches.length]=new dp.sh.Match(match[0],match.index,css);} dp.sh.Highlighter.prototype.AddBit=function(str,css) {if(str==null||str.length==0) return;var span=this.CreateElement('SPAN');str=str.replace(/ /g,' ');str=str.replace(/');if(css!=null) {if((/br/gi).test(str)) {var lines=str.split(' 
');for(var i=0;ic.index)&&(match.index/gi,'\n');var lines=html.split('\n');if(this.addControls==true) this.bar.appendChild(dp.sh.Toolbar.Create(this));if(this.showColumns) {var div=this.CreateElement('div');var columns=this.CreateElement('div');var showEvery=10;var i=1;while(i<=150) {if(i%showEvery==0) {div.innerHTML+=i;i+=(i+'').length;} else {div.innerHTML+='·';i++;}} columns.className='columns';columns.appendChild(div);this.bar.appendChild(columns);} for(var i=0,lineIndex=this.firstLine;i0;i++) {if(Trim(lines[i]).length==0) continue;var matches=regex.exec(lines[i]);if(matches!=null&&matches.length>0) min=Math.min(matches[0].length,min);} if(min>0) for(var i=0;i

Blogger Syntax Highliter

Version: {V}

http://www.dreamprojections.com/syntaxhighlighter

©2004-2007 Alex Gorbatchev.

'},ClipboardSwf:null,Version:'1.5.1'}};dp.SyntaxHighlighter=dp.sh;dp.sh.Toolbar.Commands={ExpandSource:{label:'+ expand source',check:function(highlighter){return highlighter.collapse;},func:function(sender,highlighter) {sender.parentNode.removeChild(sender);highlighter.div.className=highlighter.div.className.replace('collapsed','');}},ViewSource:{label:'view plain',func:function(sender,highlighter) {var code=dp.sh.Utils.FixForBlogger(highlighter.originalCode).replace(/'+code+'');wnd.document.close();}},CopyToClipboard:{label:'copy to clipboard',check:function(){return window.clipboardData!=null||dp.sh.ClipboardSwf!=null;},func:function(sender,highlighter) {var code=dp.sh.Utils.FixForBlogger(highlighter.originalCode).replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');if(window.clipboardData) {window.clipboardData.setData('text',code);} else if(dp.sh.ClipboardSwf!=null) {var flashcopier=highlighter.flashCopier;if(flashcopier==null) {flashcopier=document.createElement('div');highlighter.flashCopier=flashcopier;highlighter.div.appendChild(flashcopier);} flashcopier.innerHTML='';} alert('The code is in your clipboard now');}},PrintSource:{label:'print',func:function(sender,highlighter) {var iframe=document.createElement('IFRAME');var doc=null;iframe.style.cssText='position:absolute;width:0px;height:0px;left:-500px;top:-500px;';document.body.appendChild(iframe);doc=iframe.contentWindow.document;dp.sh.Utils.CopyStyles(doc,window.document);doc.write('

'+highlighter.div.innerHTML+'

');doc.close();iframe.contentWindow.focus();iframe.contentWindow.print();alert('Printing...');document.body.removeChild(iframe);}},About:{label:'?',func:function(highlighter) {var wnd=window.open('','_blank','dialog,width=300,height=150,scrollbars=0');var doc=wnd.document;dp.sh.Utils.CopyStyles(doc,window.document);doc.write(dp.sh.Strings.AboutDialog.replace('{V}',dp.sh.Version));doc.close();wnd.focus();}}};dp.sh.Toolbar.Create=function(highlighter) {var div=document.createElement('DIV');div.className='tools';for(var name in dp.sh.Toolbar.Commands) {var cmd=dp.sh.Toolbar.Commands[name];if(cmd.check!=null&&!cmd.check(highlighter)) continue;div.innerHTML+=''+cmd.label+'';} return div;} dp.sh.Toolbar.Command=function(name,sender) {var n=sender;while(n!=null&&n.className.indexOf('dp-highlighter')==-1) n=n.parentNode;if(n!=null) dp.sh.Toolbar.Commands[name].func(sender,n.highlighter);} dp.sh.Utils.CopyStyles=function(destDoc,sourceDoc) {var links=sourceDoc.getElementsByTagName('link');for(var i=0;i');} dp.sh.Utils.FixForBlogger=function(str) {return(dp.sh.isBloggerMode==true)?str.replace(/
|<br\s*\/?>/gi,'\n'):str;} dp.sh.RegexLib={MultiLineCComments:new RegExp('/\\*[\\s\\S]*?\\*/','gm'),SingleLineCComments:new RegExp('//.*$','gm'),SingleLinePerlComments:new RegExp('#.*$','gm'),DoubleQuotedString:new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),SingleQuotedString:new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'",'g')};dp.sh.Match=function(value,index,css) {this.value=value;this.index=index;this.length=value.length;this.css=css;} dp.sh.Highlighter=function() {this.noGutter=false;this.addControls=true;this.collapse=false;this.tabsToSpaces=true;this.wrapColumn=80;this.showColumns=true;} dp.sh.Highlighter.SortCallback=function(m1,m2) {if(m1.indexm2.index) return 1;else {if(m1.lengthm2.length) return 1;} return 0;} dp.sh.Highlighter.prototype.CreateElement=function(name) {var result=document.createElement(name);result.highlighter=this;return result;} dp.sh.Highlighter.prototype.GetMatches=function(regex,css) {var index=0;var match=null;while((match=regex.exec(this.code))!=null) this.matches[this.matches.length]=new dp.sh.Match(match[0],match.index,css);} dp.sh.Highlighter.prototype.AddBit=function(str,css) {if(str==null||str.length==0) return;var span=this.CreateElement('SPAN');str=str.replace(/ /g,' ');str=str.replace(/');if(css!=null) {if((/br/gi).test(str)) {var lines=str.split(' 
');for(var i=0;ic.index)&&(match.index/gi,'\n');var lines=html.split('\n');if(this.addControls==true) this.bar.appendChild(dp.sh.Toolbar.Create(this));if(this.showColumns) {var div=this.CreateElement('div');var columns=this.CreateElement('div');var showEvery=10;var i=1;while(i<=150) {if(i%showEvery==0) {div.innerHTML+=i;i+=(i+'').length;} else {div.innerHTML+='·';i++;}} columns.className='columns';columns.appendChild(div);this.bar.appendChild(columns);} for(var i=0,lineIndex=this.firstLine;i0;i++) {if(Trim(lines[i]).length==0) continue;var matches=regex.exec(lines[i]);if(matches!=null&&matches.length>0) min=Math.min(matches[0].length,min);} if(min>0) for(var i=0;i

ページビューの合計