Android界面开发基础(二)


一、EditText

在这里插入图片描述

    android:layout_width="200dp"
        android:layout_height="60dp"
        android:hint="密码"
        android:drawableLeft="@drawable/ic_baseline_account_circle_24"
        android:inputType="textPassword"
        android:drawablePadding="10dp"
        android:textColorHint="#11ffaa"
        android:paddingLeft="10dp"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

在这里插入图片描述
获取edittext中的值:

 btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String value = editText.getText().toString();
                Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
            }
        });

在这里插入图片描述

二、ImageView

在这里插入图片描述
其中缩放类型的值有以下几种:
在这里插入图片描述

这三个属性一起使用是为了保证图片以最大边进行缩放,当有一条边达到最大值后,另一条边将等比例进行缩放。

<ImageView
        android:maxHeight="300dp"
        android:maxWidth="300dp"
        android:adjustViewBounds="true"
data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

三、progressBar

在这里插入图片描述
默认是圆形的,只有加了style属性之后才是水平的。

<ProgressBar
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/pb2"
        android:max="100"
        android:indeterminate="true"

在这里插入图片描述

Notification

在这里插入图片描述
消息通知需要有两个类进行操作:
1、NotificationManager:
创建此类对象的代码:

 private NotificationManager notificationManager;
 notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2、Notification:

使用Notificationcompat类的Bulder构造器来创建Notification对象。只有这种方式可以在任何版本的android系统中运行,这是为了保障项目的健壮性。

notification = new NotificationCompat.Builder(this,"1")
                .setContentTitle("通知")
                .setContentText("正式内容,非常重要")
                .setSmallIcon(R.drawable.ic_baseline_account_circle_24)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.meinv1))
                .setColor(Color.parseColor("#ff00ff"))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();

而在使用此方法创建notification时,Builder中第二个参数需要填入一个Channelid,因此要提前创建好 NotificationChannel对象。
在这里插入图片描述

在这里插入图片描述
因为是比较的Android8.0,对应的sdk是26。
源码中可以看出VERSION_CODES.O是26。

public static final int O = 26;
//为下一步创建notification做准备。
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("1","消息",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

注意这里的id和创建notification对象中的id要保持一致,否则会报错。
在这里插入图片描述
注意在设置小图标时,图片不能有颜色。

public void sendMessage(View view){
        notificationManager.notify(1,notification);
    }

在这里插入图片描述

4、toolbar

在这里插入图片描述
toolbar是指上面这一行Education_pro,一般是在values文件夹下中的themes.xml文件中会有默认的。这里我们学习自定义的toolbar。
在这里插入图片描述
在这里插入图片描述

<androidx.appcompat.widget.Toolbar

        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"

        app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
        app:logo="@mipmap/ic_launcher"
        app:titleMarginEnd="50dp"
        app:title="aa"


        android:background="#00ff00"
        android:gravity="center"
        tools:ignore="MissingConstraints" />

五、alertdialog

在这里插入图片描述

public void showDialog(View view){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.setIcon(R.drawable.ic_launcher_background)
                .setTitle("对话框")
                .setMessage("这是一条消息")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setNeutralButton("中间",new DialogInterface.OnClickListener(){


                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create()
                .show();
    }

在这里插入图片描述

六、Popuwindow

在这里插入图片描述
将popview这个布局xml文件加载到popupwindow,popupwindow给定三个参数时,打开后无法关闭
在这里插入图片描述

public void popclick(View view) {
        View popview = getLayoutInflater().inflate(R.layout.popupwindow,null);
        PopupWindow popupWindow = new PopupWindow(popview,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
                );

        popupWindow.showAsDropDown(view);
    }

为解决该问题,只要添加一个参数即可
在这里插入图片描述

 public void popclick(View view) {
        View popview = getLayoutInflater().inflate(R.layout.popupwindow,null);
        PopupWindow popupWindow = new PopupWindow(popview,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,true
                );

        popupWindow.showAsDropDown(view);
    }

dismiss()关闭窗口的效果。

public void popclick(View view) {
        View popview = getLayoutInflater().inflate(R.layout.popupwindow,null);
        View btn1 = popview.findViewById(R.id.btn_beijing);
        View btn2 = popview.findViewById(R.id.btn_shanghai);

        PopupWindow popupWindow = new PopupWindow(popview,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,true
                );

        popupWindow.showAsDropDown(view);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值