• 5.Android开发笔记:常用控件


    1.常用控件

    TexView

    我们使用 android:gravity来指定文字的对齐方式,可选值有topbottom leftrightcenter等,可以用“|”来同时指定多个值,这里我们指定的center,效果等同于center_vertical|center_horizontal,表示文字在垂直和水平方向都居中对齐。

    Button

    细心的你可能会留意到,我们在布局文件里面设置的文字是“Button”,但最终的显示结果却是“BUTTON”。这是由于系统会对Button中的所有英文字母自动进行大写转换,如果这不是你想要的效果,可以使用如下配置来禁用这一默认特性:

    android:textAllCaps="false" 
    

    EditText

    使用 android:hint属性指定了一段提示性的文本

    imageView

    使用android:src属性给ImageView指定了一张图片。

        android:src="@drawable/img_1 "
    

    图片的宽和高都是未知的,可将ImageView的宽和高都设定为wrap_content,

        android:layout_width="wrap_content"        
        android:layout_height="wrap_content"
    

    ImageView的setImageResource()方法修改图片

     imageView  = (ImageView) findViewById(R.id.image_view); 
     imageView.setImageResource(R.drawable.img_2);
    

    ProgressBar

    用于在界面上显示一个进度条,表示我们的程序正在加载一些数据。
    所有的Android控件都具有这个属性,可以通过android:visibility进行指定,可选值有3种:visibleinvisible gone 。 visible表示控件是可见的,这个值是默认值,不指定android:visibility时,控件都是可见的。invisible表示控件不可见,但是它仍然占据着原来的位置和大小,可以理解成控件变成透明状态了。gone则表示控件不仅不可见,而且不再占用任何屏幕空间。我们还可以通过代码来设置控件的可见性,使用的是setVisibility()方法,可以传入View.VISIBLE

                    progressBar.setVisibility(View.VISIBLE);
    
                    progressBar.setVisibility(View.GONE);
    
    <ProgressBar
        android:id="@+id/pg_progressBarDefault"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <ProgressBar
        android:id="@+id/pg_progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:progressBarStyleHorizontal"
        android:visibility="gone"
        android:max="100"
        />
    <Button
        android:id="@+id/btn_ProgressBar"
        android:textAllCaps="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ProgressBar+" />
    
    //final ProgressBar progressBar = findViewById(R.id.pg_progressBar);
    Button btn_ProgressBar = findViewById(R.id.btn_ProgressBar);
    btn_ProgressBar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ProgressBar progressBar = findViewById(R.id.pg_progressBar);
            progressBar.setVisibility(View.VISIBLE);
            int progress = progressBar.getProgress();
    
            if (progress >= 100){
                progress = 0;
            }else {
                progress = progress + 10;
            }
            progressBar.setProgress(progress);
            Log.d("ProgressBar", "progress:" + progress);
        }
    });
    

    AlertDialog

    ​ 可以在当前的界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog一般都是用于提示一些非常重要的内容或者警告信息。比如为了防止用户误删重要内容,在删除前弹出一个确认对话框。

    Button btn_AlertDialog = findViewById(R.id.btn_AlertDialog);
    btn_AlertDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder dialog= new AlertDialog.Builder (FirstActivity.this);
            dialog.setTitle("This is Dialog");
            dialog.setMessage("Something important.");
            dialog.setCancelable(false);
            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(FirstActivity.this, "Ok", Toast.LENGTH_LONG)
                            .show();
                }
            });
    
            dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(FirstActivity.this, "Cancel", Toast.LENGTH_LONG)
                            .show();
                }
            });
    
            dialog.show();
        }
    });
    

    ProgressDialog

    ProgressDialog会在对话框中显示一个进度条,一般用于表示当前操作比较耗时,让用户耐心地等待。

    已经被废弃

    Button btn_ProgressDialog = findViewById(R.id.btn_ProgressDialog);
    btn_ProgressDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //ProgressDialog 已经被弃用
            ProgressDialog dialog = new ProgressDialog(FirstActivity.this);
            dialog.setTitle("标题");
            dialog.setMessage("正在加载...");
            dialog.setCancelable(true);
            dialog.show();
        }
    });
    
  • 相关阅读:
    pandas 和 matplotlib 的设置
    Django图(菜鸟教程)
    使用 pyperclip 实现复制粘贴
    Pycharm 使用 doctest 进行判断程序是否运行正常
    jieba 运行结果不显示 Building prefix dict from the default dictionary ...
    浮点数以 .0 结尾如何转换为整数
    IOS时间转时间戳出现Invalid Date的问题
    PHP小技巧
    CSS小技巧
    树状数组
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/12459244.html
Copyright © 2020-2023  润新知