/*
*放弃xml, 使用java创建安卓ui
* */
package com.example.javacreateui;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class MainActivity extends Activity {
/**
* 第一次创建Activity时回调该方法
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//创建一个线性布局
LinearLayout layout = new LinearLayout(this);
//设置该Activity显示layout
super.setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
//创建一个TextView
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容器中添加TextView
layout.addView(show);
layout.addView(bn);
//绑定一个事件监听器
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show.setText("hello,android!"+new java.util.Date().toString());
}
});
}
@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;
}
}