public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout=new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
final TextView show=new TextView(this);
Button bn=new Button(this);
bn.setText(R.string.hello_world);
bn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(show);
layout.addView(bn);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
show.setText("Hello"+new java.util.Date());
}
});
}
}
运行结果: