Android自动打字机

布局添加TextView控件,id改为code

//声明控件
String wenben="测试打字效果/换行了/再次换行/";
	int i;
	TextView code;
	private Timer timer;
	
//在布局下面粘贴

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
		code=(TextView) findViewById(R.id.code);
		final Handler handler = new Handler ( ) {
			public void handleMessage ( Message msg )
			{
				switch ( msg.what )
				{
					case 1:
						i = i + 1;
						if ( i < wenben.length ( ) )
						{
							String bu=String.valueOf ( wenben.charAt ( i ) );
							if ( bu.equals ( "/" ) )
							{
								code.setText ( code.getText ( ).toString ( ) + "\n" );
								i = i + 1;
							}
							code.append (  String.valueOf ( wenben.charAt ( i ) ) );
						}
						else
						{
							code.setText ( "" );
							i = -1;
						}
						break;
				}
				super.handleMessage ( msg );
			}
		};
		TimerTask task = new TimerTask ( ) {
			public void run ( )
			{
				Message message = new Message ( );
				message.what = 1;
				handler.sendMessage ( message );
			}
		};
		timer = new Timer ( true );
		timer.schedule ( task, 300, 300 );
		
    }
}
 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/314.html

梦雪

专注于AIDE教程分享

    留下你的评论

    *评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

    相关推荐