zoukankan      html  css  js  c++  java
  • 第一章、android入门

    安卓入门

    一、从安装到运行

    1、下载
    下载地址:https://developer.android.google.cn/studio
    
    2、安装
    安装过程中,都选默认选项安装;
    可能遇到【Unable to access Android SDK and-on list】问题,选【Cancel】,后续安装过程会安装上Android SDK。
    
    3、创建项目
    模板选【Empty Activity】;
    【Save location】为项目目录;
    【Use legacy android.support libraries】选项不要勾选。
    
    4、项目结构

    5、安装虚拟机
    右上角点击【AVD Manager】安装; 
    遇到问题【HAXM is not installed】,按提示【Install Haxm】安装即可。
    
    6、运行

    二、基础控件

    1、TextView
    <TextView
        android:id="@+id/tv_one"    :为TextView设置一个组件id
        android:layout_width="200dp"    :组件的宽度
        android:layout_height="200dp"   :组件的高度
        android:background="@color/purple_200"  :控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片
        android:gravity="center_horizontal" :设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等
        android:text="@string/hello"    :设置显示的文本内容
        android:textColor="#ffff0000"   :设置字体颜色
        android:textSize="20sp" :字体大小,单位一般是用sp
        android:textStyle="normal"  :设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
    
        android:shadowColor="@color/red"    :设置阴影颜色,需要与shadowRadius一起使用
        android:shadowDx="10.0" :设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
        android:shadowDy="10.0" :设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
        android:shadowRadius="3.0"  :设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
    
        android:singleLine="true"   :内容单行显示
        android:ellipsize="marquee" :在哪里省略文本
        android:marqueeRepeatLimit="marquee_forever"    :字幕动画重复的次数
        android:focusable="true"    :是否可以获取焦点
        android:focusableInTouchMode="true" :用于控制视图在触摸模式下是否可以聚焦>
        <requestFocus />    :获取焦点
    </TextView>
    
    2、Button
    <Button :Button继承TextView
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selector" :引用的位图,themes.xml文件DarkActionBar后需要加.Bridge
        android:backgroundTint="@color/btn_color_selector"  :引用的位图颜色
        android:onClick="llClick"   :对应MainActivity生成的llClick(View)方法
        android:text="@string/anniu"
    />
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/ic_baseline_ac_unit_24"   :引用的Drawable位图
            android:color="@color/green"    :引用的颜色
            android:state_focused="true"    :是否获得焦点
            android:state_pressed="true"    :控件是否被按下
            android:state_enabled="true"    :控件是否可用
            android:state_selected="true"   :控件是否被选择,针对有滚轮的情况
            android:state_checked="true"    :控件是否被勾选
            android:state_checkable="true"  :控件可否被勾选,eg:checkbox
        ></item>
    </selector>
    
    // 执行顺序:触摸事件(返回值为true,后续事件不触发)->长按事件(返回值为true,后续事件不触发)->点击事件
    View btn = findViewById(R.id.btn);
    // 点击事件
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("click");
        }
    });
    // 长按事件
    btn.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            System.out.println("long click");
            return false;
        }
    });
    // 触摸事件
    btn.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("touch" + event.getAction());
            return false;
        }
    });
    
    3、EditText
    <EditText
        android:id="@+id/ipt"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#99000000"  :背景色
        android:drawableLeft="@drawable/ic_baseline_person_outline_24"  :在输入框的指定方位添加图片
        android:drawablePadding="20dp"  :设置图片与输入内容的间距
        android:hint="请输入内容"    :输入提示
        android:inputType="number"  :输入类型
        android:paddingLeft="20dp"  :设置内容与边框的间距
        android:textColorHint="@color/green"    :输入提示文字的颜色
    ></EditText>
    
    // 获取输入框内容
    EditText ipt = findViewById(R.id.ipt);
    System.out.println(ipt.getText().toString());
    
    4、ImageView
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" :调整View的界限
        android:maxWidth="200dp"    :最大宽度
        android:maxHeight="200dp"   :最大高度
        android:scaleType="fitCenter"   :设置图片缩放类型
        android:src="@drawable/img" :设置图片资源
    ></ImageView>
    
    5、ProgressBar
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"    :水平进度条
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:indeterminate="false"   :如果设置成true,则进度条不精确显示进度
        android:max="100"   :进度条的最大值
        android:progress="30"   :进度条已完成进度值
        android:visibility="gone"   :进度条是否显示
    ></ProgressBar>
    
    6、Notification
    public class MainActivity extends AppCompatActivity {
        private NotificationManager manager;
        private Notification notification;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取通知管理
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断sdk是否大于8.0
                NotificationChannel channel = new NotificationChannel("ll", "通知", NotificationManager.IMPORTANCE_HIGH);//创建通知渠道,参数:(渠道id,渠道名称,通知等级)
                manager.createNotificationChannel(channel);//通知管理设置通知渠道
            }
    
            Intent intent = new Intent(this, NotificationActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
            notification = new NotificationCompat.Builder(this, "ll")//创建通知,参数:(this,渠道id)
                    .setContentTitle("官方通知")//设置通知标题
                    .setContentText("世界那么大,我想去走走")//设置通知内容
                    .setSmallIcon(R.drawable.ic_baseline_person_outline_24)//设置通知图标
                    .setColor(Color.parseColor("#00ff00"))//设置通知图标颜色
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img))//设置通知图片
                    .setContentIntent(pendingIntent)//设置通知点击后跳转页面
                    .setAutoCancel(true)//设置通知点击后关闭
                    .build();
        }
    
        public void sendNotice(View view) {
            manager.notify(1, notification);//发送通知
        }
    
        public void cancelNotice(View view) {
            manager.cancel(1);//关闭通知
        }
    }
    
    7、Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffff0000"
        app:logo="@drawable/ic_baseline_ac_unit_24"
        app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
        app:subtitle="子标题"
        app:subtitleTextColor="@color/black"
        app:title="标题"
        app:titleMarginStart="100dp"
        app:titleTextColor="@color/green">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="标题" />
    </androidx.appcompat.widget.Toolbar>
    
    8、AlertDialog
    public void dialogButton(View view) {
        View dialogView = getLayoutInflater().inflate(R.layout.activity_notification, null);//获取自定义布局
    
        AlertDialog.Builder builder = new AlertDialog.Builder(this);//构建Dialog的各种参数
        builder.setIcon(R.drawable.ic_baseline_person_outline_24)//添加ICON
                .setTitle("弹窗")//添加标题
                .setMessage("这是一段中文")//添加消息
                .setView(dialogView)//设置自定义布局
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("", "点击了确定");
                    }
                })//确定按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("", "点击了取消");
                    }
                })//取消按钮
                .setNeutralButton("中间", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.e("", "点击了中间");
                    }
                })//中间按钮
                .create()//创建Dialog
                .show();//显示对话框
    }
    
    9、PopupWindow
    public void popupButton(View view) {
        View popupView = getLayoutInflater().inflate(R.layout.activity_popupwindow, null);//获取自定义布局
        Button btn1 = popupView.findViewById(R.id.btn1);
        Button btn2 = popupView.findViewById(R.id.btn2);
    
        PopupWindow popupWindow = new PopupWindow(popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true);//参数:(设置PopupWindow显示的View,宽,高,设置是否获取焦点)
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.img));//设置背景
        popupWindow.showAsDropDown(view);//相对某个控件的位置(正左下方),无偏移
    
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("", "点击确定");
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("", "点击取消");
                popupWindow.dismiss();//关闭弹窗
            }
        });
    }
    
  • 相关阅读:
    c#MD5珍藏
    html跳转倒计时
    微信支付.NET版开发总结(JS API),好多坑,适当精简
    zip文件jQuery工作地点选择城市代码
    百度地图Api进阶教程-点击生成和拖动标注4.html
    MySQL注入
    SQL语法:inner join on, left join on, right join on详细使用方法
    Mysql INNER,LEFT ,RIGHT join的使用
    让Sql语句区分大小写
    关于try和finaly 里面return的问题
  • 原文地址:https://www.cnblogs.com/linding/p/15437885.html
Copyright © 2011-2022 走看看