Android Button使用,ArrayList使用练习

package com.mowo;

 

import java.util.ArrayList;

import java.util.Iterator;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;

 

public class TestWorkspace extends Activity implements OnClickListener {

 

private Button mButton1;

private Button mButton2;

private Button mButton3;

/** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

 

        initViews();

    }

 

    @Override

    protected void onResume() {

        super.onResume();

 

TextView textView = (TextView)findViewById(R.id.hello);

        textView.setText("Android practice");

    }

 

    private void initViews() {

    

mButton1 = (Button)findViewById(R.id.button1);

mButton1.setOnClickListener(this);

 

mButton2 = (Button)findViewById(R.id.button2);

mButton2.setOnClickListener(this);

mButton3 = (Button)findViewById(R.id.button3);

mButton3.setOnClickListener(this);

    }

 

public void onClick(View view) {

 

StringBuffer buffer = new StringBuffer();

switch (view.getId()) {

case R.id.button1: 

buffer.append("我是Android ");

buffer.append("别叫我symbian");

Toast.makeText(this, buffer.toString(), Toast.LENGTH_SHORT).show();

break;

case R.id.button2:

TextCache cache = new TextCache();

buffer.append(cache.a);

buffer.append(cache.b);

Toast.makeText(this, buffer.toString(), Toast.LENGTH_SHORT).show();

break;

case R.id.button3:

// buffer.append("威哥 ");

// buffer.append("你真棒 你家的猫更棒!");

// Toast.makeText(this, buffer.toString(), Toast.LENGTH_SHORT).show();

arrayListText(buffer);

break;

default:

break;

}

}

 

final static class TextCache {

String a = "诺基亚 + 微软 = ";

String b = " 一个更比一个惨";

}

// Android ArrayList练习

public void arrayListText(StringBuffer buffer){

String mStr1 = "Hello1", mStr2 = "Hello2", mStr3 = "Hello3", mStr4 = "Hello4", mStr5 = "Hello5",

      mStr6 = "Hello6", mStr7 = "Hello7", mStr8 = "Hello8", mStr9 = "Hello9", mStr10 = "Hello10";

ArrayList<String> strArray = new ArrayList<String>();

strArray.add(mStr1);

strArray.add(mStr2);

strArray.add(mStr3);

strArray.add(mStr4);

strArray.add(mStr5);

strArray.add(mStr6);

strArray.add(mStr7);

strArray.add(mStr8);

strArray.add(mStr9);

strArray.add(mStr10);

StringBuffer allHello = new StringBuffer();

StringBuffer Hello13579 = new StringBuffer();

StringBuffer Hello246810 = new StringBuffer();

Iterator<String> it = strArray.iterator();

int i = 1;

do{

if(i % 2 == 1){

Hello13579.append(it.next());

Hello13579.append(" ");

i ++;

}

else{

Hello246810.append(it.next());

Hello246810.append(" ");

i ++;

}

}while(it.hasNext());

allHello.append(Hello13579);

allHello.append(Hello246810);

buffer.append(allHello);

Toast.makeText(this, buffer.toString(), Toast.LENGTH_SHORT).show();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值