一个点击添加button的例子

Java:

public class ScrollViewButton extends Activity {
	/** Called when the activity is first created. */
	private LinearLayout mLayout;
	private ScrollView mScrollView;
	private final Handler mHandler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        //隐去电池等图标和一切修饰部分(状态栏部分)   
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
        // 隐去标题栏(程序的名字)  
		setContentView(R.layout.test);
		// 创建一个线性布局
		mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);
		// 创建一个ScrollView对象
		mScrollView = (ScrollView) this.findViewById(R.id.ScrollView);
		Button mBtn = (Button) this.findViewById(R.id.Button);
		mBtn.setOnClickListener(mClickListener);// 添加点击事件监听
	}

	public boolean onKeyDown(int keyCode, KeyEvent event){
		Button b = (Button) this.getCurrentFocus();
		int count = mLayout.getChildCount();
		Button bm = (Button) mLayout.getChildAt(count-1);

		if(keyCode==KeyEvent.KEYCODE_DPAD_UP && b.getId()==R.id.Button){
			bm.requestFocus();
			return true;
		}else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && b.getId()==bm.getId()){
			this.findViewById(R.id.Button).requestFocus();
			return true;
		}
		return false;
	}
	// Button事件监听,当点击第一个按钮时增加一个button和一个textview
	private Button.OnClickListener mClickListener = new Button.OnClickListener() {

		private int index = 1;

		@Override
		public void onClick(View v) {
			TextView tView = new TextView(ScrollViewButton.this);//定义一个TextView
			tView.setText("TextView" + index);//设置TextView的文本信息
			//设置线性布局的属性
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			mLayout.addView(tView, params);//添加一个TextView控件
			Button button = new Button(ScrollViewButton.this);//定义一个Button
			button.setText("Button" + index);//设置Button的文本信息
			button.setId(index++);
			mLayout.addView(button, params);//添加一个Button控件
			mHandler.post(mScrollToButton);//传递一个消息进行滚动
		}

	};
	private Runnable mScrollToButton = new Runnable() {

		@Override
		public void run() {
			int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();
			if (off > 0) {
				mScrollView.scrollTo(0, off);//改变滚动条的位置
			}
		}

	};
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/ScrollView" 
	android:layout_width="fill_parent"
	android:layout_height="wrap_content" 
	android:scrollbars="vertical">
	<LinearLayout 
		android:id="@+id/LinearLayout"
		android:orientation="vertical" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
		 	android:id="@+id/TestView" 
			android:layout_width="fill_parent"
			android:layout_height="wrap_content" 
			android:text="TestView0" />
		<Button 
			android:id="@+id/Button" 
			android:text="Button0"
			android:layout_width="fill_parent" 
			android:layout_height="wrap_content"></Button>
	</LinearLayout>
</ScrollView>

运行结果:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值