使用纯代码的界面程序

package org.crazyit.codeview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CodeView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /*布局*/
        LinearLayout layout = new LinearLayout(this);
        super.setContentView(layout);
        layout.setOrientation(LinearLayout.VERTICAL);
        
        /*TextView控件*/
        final TextView show = new TextView(this);
        show.setText("显示时间!");
        
        /*Button控件*/
        Button bn = new Button(this);    
        bn.setText("单击我");
        bn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT     
        ));
        
        /*添加到布局上*/
        layout.addView(show);
        layout.addView(bn);
        
        /*对按钮设置事件*/
        bn.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                show.setText("Hello World"+new java.util.Date());
            }
        });
        
        //setContentView(R.layout.main);
    }
}

你可能感兴趣的:(Android,button,layout,class)