package com.example.eg16_3;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	
	LinearLayout linearLayout;
	LinearLayout.LayoutParams param;
	
	Button button1;
	Button button2;
	Button button3;
	Button button4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);	

		button1 = (Button)findViewById(R.id.button1);
		button1.setOnClickListener(this);
		
		button2 = (Button)findViewById(R.id.button2);
		button2.setOnClickListener(this);
		
		button3 = (Button)findViewById(R.id.button3);
		button3.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "我是Button3", Toast.LENGTH_SHORT).show();
			}  
		});
		
		// 动态创建button4
		linearLayout = (LinearLayout)findViewById(R.id.linearLayout01);
		param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		LinearLayout layout1 = new LinearLayout(this);
		layout1.setOrientation(LinearLayout.HORIZONTAL);
		
		button4 = new Button(this);
		button4.setId(100);
		button4.setText("动态创建的Button");
		button4.setTextColor(Color.GREEN);
		
		layout1.addView(button4, param);
		linearLayout.addView(layout1, param);
		
	}
	
	public void onClick(View v) {
 		switch(v.getId()){
     	 case R.id.button1:
      	 	Toast.makeText(this, "我是Button1", Toast.LENGTH_SHORT).show();
      	 	break;
      	 	
     	case R.id.button2:
      	 	Toast.makeText(this, "我是Button2", Toast.LENGTH_SHORT).show();
      	 	break;
 		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
