当前位置: 首页 > >

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 {
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 {
views[i]=findViewById(names[i]);
}
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
},0,200);


}
}

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


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

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"
/>
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"
/>
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"
/>
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"
/>
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"
/>
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"
/>

?


?


?


?


?


?


?


?


?


?



友情链接: