美文网首页
Android - 基础控件

Android - 基础控件

作者: 疯狂小跳虫 | 来源:发表于2017-12-20 16:35 被阅读30次
TextView

显示文本控件

//在xml中的代码:
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"     //显示文本
        android:gravity="center"     //内容居中 ,可选值有top、bottom、left、right、center等
        android:textSize = "24sp"   //字体大小
        android:textColor = "#00ff00"  //字体颜色
        tools:layout_editor_absoluteX="30dp"
        tools:layout_editor_absoluteY="40dp"/>

//android:layout_width 和android:layout_height是所有控件都有的属性,可选值有:
//match_parent、fill_parent:当前控件大小和父布局大小相同
//wrap_content :内容决定控件大小
Button

按钮控件

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"    //设置大小写
        tools:layout_editor_absoluteX="212dp"
        tools:layout_editor_absoluteY="34dp"/>
//第一种方法添加点击事件
        Button button = (Button)findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加点击事件
            }
        });
//第二种方法添加点击事件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thrid);
        Button button = (Button)findViewById(R.id.button2);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //添加点击事件
    }
EditText

编辑文本控件
在xml文件中用法相似,只写出区别属性

android:hint="Type something here" //提示文本 
androis:maxLines="2"  //最大行数
EditText editText=(EditText)findViewById(R.id.edit_text);
String text = editText.getText().toString();
ImageView

显示图片

app:srcCompat="@mipmap/ic_launcher"//显示那张图片
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.mipmap.ic_launcher_round);
//动态修改显示图片
ProgressBar

进度条显示
android:visibility。指定控件是否显示,可选值
visible:可见。默认值
invisible:不可见,但仍占据着控件原有位置和大小
gone:不可见,不占据位置

//xml文件
style="?android:attr/progressBarStyle" //样式
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
AlertDialog

弹出提示对话框

        AlertDialog.Builder dialog = new AlertDialog.Builder(ThridActivity.this);
        dialog.setTitle("Dialog");
        dialog.setMessage("Something important.");
        dialog.setCancelable(false);
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        dialog.show();

相关文章

  • Android 基础

    Android基础01控件和布局 Android基础02数据存储 Android基础03网络编程 Android基...

  • SeekBar

    Android-SeekBar进度条的使用Android控件与布局——基础控件SeekBar

  • android 基础控件通用属性

    android 基础控件通用属性

  • Android - Navigation

    Android 基础知识 1. Android 常用控件 2. 控件常用属性 Android 常用知识点 动态权限...

  • 第一行代码(2)UI设计

    1. 基础UI控件 Android中的基础UI控件有这样几种: TextView Button EditText ...

  • Android view位移滑动

    Android view的位移滑动 基础 Android view的移动不管特效多么绚丽,都是view控件的基础移...

  • Android自定义控件之自定义组合控件

    Android自定义控件之自定义组合控件 前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原...

  • Android - 基础控件

    TextView 显示文本控件 Button 按钮控件 EditText 编辑文本控件在xml文件中用法相似,只写...

  • 第五章 Android 常见UI基础控件(一)

    这里主要讲讲新的Android 基础的UI控件,此外还拓展下新的控件。所有的控件都是View的子类。常见的UI控件...

  • View的事件体系

    View基础知识 什么是View Android中的控件主要分为容器控件和普通控件,它们都继承View父类,容器控...

网友评论

      本文标题:Android - 基础控件

      本文链接:https://www.haomeiwen.com/subject/lhjgwxtx.html