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 );
		
    }
}
© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
评论 抢沙发

请登录后发表评论