【安卓开发-记录】

Base

1.导包

import 包名.*;


.*获取某个目录下所有类

【整理代码】
int 随机数 = new Random().nextInt();
		

基础

$变量修饰符号
【String】字符串(需要"内容")
【long】长数型642进制(原来32)double】浮点型(精确)小数
【float】浮点型(不精确)boolean】布尔型(true,false)int】整型(整数)
【void】无类型
写法:
类型 名称=内容;
int a=666;
String a="666"

$运算
【+*/(只得到整数)  
100/40=2
 (添加浮点得小数:一个(.)即可)
100.0/40.0=2.5
【比较大小】
大于:>
小于:<
等于:==
不等于:!=
综合:<=(小于等于:)
字符串比较:
等于:equals
写法:"hello".equals("hello")

输出  布尔型:true/false


i++;   等于int i=i+1;



$判断
【if/else/else ifif(判断){
//判断正确执行
}

写法(判断是否为偶数):
if(100%2==0){}


赋值写法
a=b;while循环】
i+=a;(将所有a的值相加)for循环:while的简化】
for(int 变量=赋值;变量判断;变量执行){}

数组


               类型[] 名称 = { 子集 };
	$索引调用:名称[0]//首项序号为0

写法:
int[] a = { 4, 8, 2 };
		System.out.println(a[0]);


【遍历访问数组:访问所有】

    int[] a = { 5,7,3,9};
		for (int i = 0; i < a.length; i++)
		{//a.ength数组总长度(总数)
			System.out.println(a[i]);
		}
【空型数组】
int[] a = new int[4];//4为总长度(默认值为0)
a[0] = 5;//索引改变默认赋值



Color_inject
【#型调用】
$本来UI选择时为#FF00D8FF
Java中用时要把#改为0x

例子:
0xFF00D8FF

【字符串资源型】
1.res/value/color.xml

添加

<color name="颜色调用名称">UI色</color>


$代码调用
this.getResources().getColor(R.color.颜色调用名称)


amin-xml
【ainm】
(渐显a.xml)
<?xml version="1.0" encoding="utf-8"?>
<alpha
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:fromAlpha="1"
	android:toAlpha="0"
	android:duration="1000">
	
</alpha>


(移动b.xml)

<?xml version="1.0" encoding="utf-8"?>
<translate
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:fromXDelta="0"
	android:toXDelta="200"
	android:fromYDelta="0"
	android:toYDelta="200"
	android:duration="1000">

</translate>


amin-java
【安卓动画】

public void dh(int id,String a){
	  if(a=="x"){
		findViewById(id).startAnimation(AnimationUtils.loadAnimation(this,R.anim.a));
	  }if(a=="y"){
		findViewById(id).startAnimation(AnimationUtils.loadAnimation(this,R.anim.b));
		}
		}
	                                 
	public  void  渐显dh(int id){
		                                                                   
		Animation 渐显 =new AlphaAnimation(0,1);
		渐显.setDuration(1000);                                              
		findViewById(id).startAnimation(渐显);
		提示("渐显动画");
	}
封装
【修饰符号】
private(内部ss全局变量,无法被外部类进行访问)


public
protected
【注意】
$在类中定义为内部全局变量(private)
外部不可访问

$如果要进行访问
需要在内方法前加入(public)进行修饰
class Rectangle
{
	private int width;
	private int height;
	



Class-Object

【调用】
类名 对象名=new 类名();
对象名.a=参数;



【类】
class 类名{
int a;
int b;//定义变量

//添加构造器,直接用方法
类名(int a,int b){
        this.a=a;
        this.b=b;//初始化(将传入的参数赋值)

}





}



【连环类实例】


Rectangle rect = new Rectangle();
		rect.width = 100;
		System.out.println(rect.width);
		rect.height = 200;
		System.out.println(rect.height);
		
		Rectangle rect2 = new Rectangle();
		rect2.width = 10;
		System.out.println(rect2.width);
		rect2.height = 20;
		System.out.println(rect2.height);
		
		Point p = new Point();
		p.x = 4;
		p.y = 5;
		System.out.println(p.x);
		System.out.println(p.y);
		rect2.position=p;
        System.out.println(rect2.position.x);
        System.out.println(rect2.position.y);
	

$类
class Point
{
	int x;
	int y;
}

class Rectangle
{
	int width;
	int height;
	Point position;
}





	



Class-Extends
$【extends:继承】

1.子类 extends  父类(继承)
2.提供父类的构造方法(super)

子类{
public 子类(参数){
super(参数);//这个是调用父类的
}

}




【多态】

用子类创建的对象 
开头的类型可使用父类类型
比如:

子类 a=new 子类(参数);//原来的
父类 a=new 子类(参数);//现在的
$使用多态后不能调用子类的方法




【实例】

public static void main(String[] args)
	{
		Rectangle rect = new Rectangle(100, 200);
		System.out.println(rect.getArea());
		
		Rectangle square = new Square(100);//这里是多态
		System.out.println(square.getArea());
	}
}

class Square extends Rectangle
{
	public Square(int size)
	{
		super(size, size);
	}
}

class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

widget-Adapter
String[] 数组 = new String[] { "Aide", "iApp", "AndLua",
        	"JAVA", "Spring", "C", "C++", "物理", "化学" };
        	
        ListAdapter 适配器 = new 我的适配器(this, 数组);
        
        ListView listView = (ListView) findViewById(R.id.mainListView1);
		listView.setAdapter(适配器);
        
    }
    
    class 我的适配器 extends ArrayAdapter<String>
    {
		public 我的适配器(Context 上下文, String[] 参数) 
		{
			super(上下文, R.layout.entry, 参数);
		}

		@Override
		public View getView(int 位置, View convertView, ViewGroup 父类)
		{
			LayoutInflater 填充器 = LayoutInflater.from(getContext());
			View 填充布局 = 填充器.inflate(R.layout.entry, 父类, false);
			
			String 取文本 = getItem(位置);
	
			TextView 显文本 = (TextView) 填充布局.findViewById(R.id.entryTextView1);
			显文本.setText(取文本);
	
			ImageView 图片 = (ImageView) 填充布局.findViewById(R.id.entryImageView1);
			图片.setImageResource(android.R.drawable.ic_menu_info_details);
			
			if("Google".equals(取文本))
				图片.setImageResource(android.R.drawable.ic_menu_gallery);
			
			return 填充布局;
		}
    }
Widget_Notifi
//发送通知
public  void 发送通知(){
        
            // 构建意图
            Intent intent = new Intent(this, MainActivity.class);
            PendingIntent pi = PendingIntent.getActivities(this, 0, new Intent[]{intent}, 0);
            // 获取系统通知管理服务
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // 构建 Notification
            Notification.Builder notification = new Notification.Builder(this)
                .setContentTitle("这里是通知标题")
                .setContentText("震惊!男人看了会沉默,女人看了会流泪")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.abc_btn_check_material)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.abc_btn_borderless_material))
                .setContentIntent(pi) // 跳转
                .setAutoCancel(true) // 取消通知
                .setSound(Uri.fromFile(new File("/system/media/audio/notifications/Simple.ogg"))) // 通知铃声
                .setVibrate(new long[] {0, 1000, 1000, 1000}) // 通知时震动
                .setLights(Color.GREEN, 1000, 1000) // LED灯
                ;
            // 兼容  API 26,Android 8.0
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // 第三个参数表示通知的重要程度,默认则只在通知栏闪烁一下
                NotificationChannel notificationChannel = new NotificationChannel("AppTestNotificationId", "AppTestNotificationName", NotificationManager.IMPORTANCE_DEFAULT);
                // 注册通道,注册后除非卸载再安装否则不改变
                manager.createNotificationChannel(notificationChannel);
                notification.setChannelId("AppTestNotificationId");
            }
            // 发出通知
            manager.notify(1, notification.build());
            
        }
Widget_Notifi_2
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder notification = new Notification.Builder(MainActivity.this);
        notification.setAutoCancel(true);
        notification.setSmallIcon(R.drawable.ic_launcher);
        notification.setContentTitle("标题");
        notification.setContentText("通知的内容");
        notification.setDefaults(Notification.DEFAULT_SOUND |Notification.DEFAULT_VIBRATE);
        notification.setWhen(System.currentTimeMillis());
        Intent intent=new Intent(MainActivity.this,MainActivity.class);
        PendingIntent pi= PendingIntent.getActivity(MainActivity.this,0,intent,0);
        notification.setContentIntent(pi);
        notificationManager.notify(NOTIFYID,notification.build());


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
步数记录器可以通过以下步骤实现: 1. 添加传感器权限和计步器传感器类型声明到AndroidManifest.xml文件中。 ``` <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" /> <uses-feature android:name="android.hardware.sensor.stepcounter" /> <uses-feature android:name="android.hardware.sensor.stepdetector" /> ``` 2. 在Activity中获取传感器服务的实例。 ``` SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); ``` 3. 获取计步器传感器的实例,并注册监听器。 ``` Sensor stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); sensorManager.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 4. 在监听器中,实现步数的记录和更新。 ``` @Override public void onSensorChanged(SensorEvent event) { Sensor sensor = event.sensor; if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) { int stepCount = (int) event.values[0]; updateStepCount(stepCount); } } private void updateStepCount(int stepCount) { // 记录步数并更新UI mStepCount = stepCount; mStepCountTextView.setText(String.valueOf(mStepCount)); } ``` 5. 在Activity中,可以添加计时器,定期记录步数并更新UI。 ``` private Timer mTimer; private TimerTask mTimerTask; private void startRecord() { mTimer = new Timer(); mTimerTask = new TimerTask() { @Override public void run() { updateStepCount(mStepDetector.getStepCount()); } }; mTimer.schedule(mTimerTask, 0, 1000); } private void stopRecord() { if (mTimer != null) { mTimer.cancel(); mTimer = null; } if (mTimerTask != null) { mTimerTask.cancel(); mTimerTask = null; } } ``` 以上是一个简单的步数记录器的实现示例。需要注意的是,计步器传感器的精度和准确性可能会受到设备硬件和软件版本的影响,因此需要进行测试和验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值