1. 程式人生 > >Android入門小列子,介面程式設計

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<names.length;i++)
                {
                    views[i].setBackgroundResource(colors[(i+currentColor)%names.length]);
                }
                currentColor++;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for (int i=0;i<names.length;i++)
        {
            views[i]=findViewById(names[i]);
        }
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x123);
            }
        },0,200);


    }
}

佈局檔案:(採用幀佈局FrameLayout)

    <FrameLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="320px"
            android:height="320px"
            android:background="#f00"
             />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="280px"
            android:height="280px"
            android:background="#0f0"
            />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="240px"
            android:height="240px"
            android:background="#00f"
            />
        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="200px"
            android:height="200px"
            android:background="#ff0"
            />
        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="160px"
            android:height="160px"
            android:background="#f0f"
            />
        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:width="120px"
            android:height="120px"
            android:background="#0ff"
            />
    </FrameLayout>