Android入门之ImageButton(纯java)

 Android入门之ImageButton(纯java)_第1张图片    Android入门之ImageButton(纯java)_第2张图片

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class ImageButtonTest extends Activity {
	
	private RelativeLayout mainLayout=null;	//主容器
	private TextView tv=null;				//文本
	private Button button=null;				//普通文字按钮
	private ImageButton ib=null;			//图片按钮
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainLayout_init();
        setContentView(mainLayout);//显示mainLayout
    }
    /*mainLayout初始化*/
    void mainLayout_init(){
    	mainLayout=new RelativeLayout(this);
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-1,-1);
        mainLayout.setLayoutParams(lp);
        tv_init();
        button_init();
        ib_init();
        mainLayout.addView(tv);
        mainLayout.addView(button);
        mainLayout.addView(ib);
    }
    /*tv初始化*/
    void tv_init(){
    	tv=new TextView(this);
    	RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-1, -2);
    	lp.addRule(RelativeLayout.CENTER_VERTICAL);//垂直居中
    	tv.setLayoutParams(lp);
    	tv.setText("这里显示案件相应");
    }
    /*buton初始化*/
    void button_init(){
    	button=new Button(this);
    	RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);
    	button.setLayoutParams(lp);
    	//设定文字
    	button.setText("普通文字按钮");
    	//绑定单击监听
    	button.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				tv.setText("你单击了普通文字按钮");
			}
		});
    }
    /*ib初始化*/
    void ib_init(){
    	ib=new ImageButton(this);
    	RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(-2, -2);
    	lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//居底
    	lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//居右
    	ib.setLayoutParams(lp);
    	//设定图片
    	ib.setImageResource(this.getResources().getIdentifier("icon", "drawable", getPackageName()));
    	//绑定单击监听
    	ib.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				tv.setText("你单击了图片按钮");
			}
		});
    }
}


 

你可能感兴趣的:(java,android,Class,import,button)