Android Button使用,ArrayList使用练习

    技术2025-05-31  7

    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)