package com.example.eg16_4;

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.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	
	LinearLayout linearLayout;
	LinearLayout.LayoutParams param;
	
	TextView textview;
	
	ImageButton imagebutton1;
	ImageButton imagebutton2;
	ImageButton imagebutton3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textview = (TextView)findViewById(R.id.textView1);
		
		
		imagebutton1 = (ImageButton)findViewById(R.id.button1);
		imagebutton2 = (ImageButton)findViewById(R.id.button2);
		//注册监听
		imagebutton1.setOnClickListener(this);
		imagebutton2.setOnClickListener(this);
		
		// 动态创建imagebutton3
		linearLayout = (LinearLayout)findViewById(R.id.linearLayout01);
		param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		
		imagebutton3 = new ImageButton(this);
		imagebutton3.setId(100);
		//设置自己的图片
		imagebutton3.setBackgroundDrawable(getResources().getDrawable(R.drawable.p2));
		//注册监听
		imagebutton3.setOnClickListener(this);
		
		layout.addView(imagebutton3, param);
		linearLayout.addView(layout, param);	
	}
	
	public void onClick(View v) {
 		switch(v.getId()){
     	 case R.id.button1:
     		textview.setText("您刚才点击的是ImageButton1");
      	 	break;
      	 	
     	case R.id.button2:
     		textview.setText("您刚才点击的是ImageButton2");
      	 	break;
      	 	
     	case 100:
     		textview.setText("您刚才点击的是ImageButton3");
      	 	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;
	}

}
