标签:
1 public class MainActivity extends Activity { 2 //当第一次创建Activity时回调该方法 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 //创建一个线性布局管理器 7 LinearLayout layout = new LinearLayout(this); 8 //设置该Activity显示layout 9 super.setContentView(layout); 10 layout.setOrientation(LinearLayout.VERTICAL); 11 //创建一个TextView 12 final TextView show=new TextView(this); 13 //创建一个按钮 14 Button bn = new Button(this); 15 bn.setLayoutParams(new ViewGroup.LayoutParams( 16 ViewGroup.LayoutParams.WRAP_CONTENT, 17 ViewGroup.LayoutParams.WRAP_CONTENT 18 )); 19 //向layout容器中添加TextView 20 layout.addView(show); 21 //行layout容器中添加一个按钮 22 layout.addView(bn); 23 //为按钮绑定一个事件监听 24 bn.setOnClickListener(new View.OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 show.setText("Hello,Android,"+new Date()); 28 } 29 }); 30 } 31 }
标签:
原文地址:http://www.cnblogs.com/LY1124/p/4665323.html