Android学习:代码控制UI界面示例

package allegro.test2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout=new LinearLayout(this);
        super.setContentView(layout);
        layout.setOrientation(LinearLayout.VERTICAL);
        final TextView show=new TextView(this);
        Button bn=new Button(this);
        bn.setText("lxy btn"); //R.string.app_name);
        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)
            {
                show.setText("Hello,android,"+new java.util.Date());
            }
        });
        //setContentView(R.layout.activity_main);
    }
}

 

 

posted @ 2018-03-13 21:30  猪悟能  阅读(312)  评论(0编辑  收藏  举报