Android入门小列子,界面编程

1.用编程的方式开发界面:

super.onCreate(savedInstanceState);
        LinearLayout layout=new LinearLayout(this);//创建一个线性布局管理器
        //设置该Activity显示layout
        super.setContentView(layout);
        layout.setOrientation(LinearLayout.VERTICAL);
        final TextView show=new TextView(this);
        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 View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show.setText("Hello android,"+new java.util.Date());
            }
        });

2.跟随手指的小球:

DrawView类中的代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View {
    public float currentX=40;
    public float currentY=50;
    Paint p=new Paint();//定义并创建画笔
    public DrawView(Context context){
        super(context);
    }
    public DrawView(Context context, AttributeSet set){
        super(context,set);
    }

    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        //设置画笔颜色
        p.setColor(Color.RED);
        //绘制小球
        canvas.drawCircle(currentX,currentY,15,p);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
        //修改currentX,Y,属性
        currentX=event.getX();
        currentY=event.getY();
        //通知组件重绘自己
        invalidate();
        return true;
    }

}

MainActivity中的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout root=findViewById(R.id.root);
        final DrawView draw=new DrawView(this);
        draw.setMinimumHeight(500);
        draw.setMinimumWidth(300);
        root.addView(draw);

    }
}

在此布局文件省略。。

3。霓虹灯效果:

MainActivity中的代码:

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private int  currentColor=0;
    final int[] colors=new int[]{
            R.color.color1,
            R.color.color2,
            R.color.color3,
            R.color.color4,
            R.color.color5,
            R.color.color6
    };
    final int[] names=new int[]{
            R.id.textView1,
            R.id.textView2,
            R.id.textView3,
            R.id.textView4,
            R.id.textView5,
            R.id.textView6
    };
    TextView[] views=new TextView[names.length];
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
            if(msg.what==0x123)
            {
                for (int i=0;i

布局文件:(采用帧布局FrameLayout)

    

        
        
        
        
        
        
    

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(编程,Android)