android--day02(UI、事件)

来源:互联网 发布:java监听器使用方法 编辑:程序博客网 时间:2024/06/03 16:42

布局绍介:
两种方式声明布局:
1.在xml中声明ui元素
2.在运行是实例化布局元素(代码中new出来的)

linnerLayout(线性布局)
水平方向
垂直方向
这里写图片描述

relativeLayout(相对布局,相对其它组件的布局)

这里写图片描述

这里写图片描述

这里写图片描述

TableLayout(表格布局:按照行列方式布局)

FrameLayout(帧布局)

absoluteLayout(绝对布局:按照绝对坐标布局)

gridLayout(网络布局)
任意摆放


Toast组件

显示文本

    /**     * 按扭的单击事件方法     * @param v     */    public void viewText(View v){        //getApplicationContext :应用程序上下文,作用域为整个程序        //this :当前对象(当前界面的上下文)        Toast.makeText(getApplicationContext(),"欢迎你!",Toast.LENGTH_SHORT).show();    }

显示图片

        Toast t  = new Toast(this);        ImageView imageView = new ImageView(this);        imageView.setImageResource(R.drawable.photo2);        t.setView(imageView);        t.setDuration(Toast.LENGTH_SHORT);        //显示的位置        t.setGravity(Gravity.CENTER,0,0);        t.show();

图片和文本显示

 public  void  viewImageText(View v){        Toast t = new Toast(this);        //文本组件        TextView textView = new TextView(this);        textView.setText("可爱很哦");        //图片组件        ImageView imageView = new ImageView(this);        imageView.setImageResource(R.drawable.photo2);        //线性布已局        LinearLayout layout = new LinearLayout(this);        layout.setOrientation(LinearLayout.VERTICAL);        layout.addView(imageView);        layout.addView(textView);        t.setView(layout);        //显示的位置        t.setGravity(Gravity.CENTER, 0, 0);        t.setDuration(Toast.LENGTH_SHORT);        t.show();    }
属性说明:显示的文本可以选择复制: android:textIsSelectable="true"提示输入: android:hint="请输入……"输入框添加图片:android:drawableLeft="@drawable/photo2"是否可编辑:android:editable="true"不显示组件: android:visibility="gone"不显示组件,但占位:  android:visibility="invisible"只输入数字:android:inputType="number"
编辑器的事件: editText = (EditText) findViewById(R.id.editText); //监听文本输入变化 事件 editText.addTextChangedListener(new TextWatcher() {            @Override            public void beforeTextChanged(CharSequence s, int start, int count, int after) {            //输入之前文本框中的内容            }            @Override            public void onTextChanged(CharSequence s, int start, int before, int count) {            //当输入时文本框中的内容            }            @Override            public void afterTextChanged(Editable s) {            //当输入后文本框中的内容            }        });//监听回车确认键 事件: editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {            @Override            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {            Toast.makeText(MainActivity.this,v.getText().toString(),Toast.LENGTH_SHORT).show();                return false;            }        });
Button 常用属性:OnClickListener 事件:1.本类实现 View.OnClickListener 接口2.使用内部类         b3 = (Button)findViewById(R.id.button3);        b3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this,"内部类",Toast.LENGTH_SHORT).show();            }        });3.自定义方法,配置android:onclick属性配置透明的Button1.style = "?android:attr/borderlessButtonStyle"配置Button Style1.通过android:background来设置按扭样式使用XML drawable selector1.drawable下创建资源文件:<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!--按下时按扭的颜色-->    <item android:state_pressed="true" android:drawable="@color/red"></item>    <!--按扭默认的颜色-->    <item android:drawable="@color/gren"></item></selector>2.在按钮中引用样式: android:background="@drawable/botton_bg"
ImageView:显示一个任意的图像,可以加载图像从不同的资源xml属性:android:src: 设置View的drawable(如图片)anddroid:adjustViewBounds:是否保特宽高比。需要与maxWidth、MaxHeight 一起使用,否则单独使用没有效果android:maxHeight:设置View的最大高度,单独使用无效,需要与setAdjustViewBounds 一起使用如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:1.设置setAdjustViewBounds 为 True2.设置maxWidth MaxHeight;3.设置layout_width 和 layout_height 为wrap_content    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/imageView"        android:src="@mipmap/ic_launcher"        android:maxHeight="200dp"        android:maxWidth="200dp"        android:adjustViewBounds="true"        />android:maxWidth: 设置View的最大宽度。

这里写图片描述

CheckBox:复选框接口:OnCheckedChangeListener处理事件:onCheckedChangedRaidoButton:单选按扭:必须入在RadioGroup内接口:OnCheckedChangeListener处理事件:onRadioButtonClicked
ToggleButton(切换按钮)  <ToggleButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="开关扭"        android:id="@+id/toggleButton"        android:layout_alignParentTop="true"        android:layout_alignParentStart="true"        android:layout_marginTop="82dp"        android:textOff="关"        android:textOn="开"        android:checked="true"        />Switches(4.0以后)RatingBar(评级)<RatingBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/ratingBar"        android:layout_alignParentBottom="true"        android:layout_alignParentStart="true"        android:layout_marginBottom="54dp"        //步长        android:stepSize="1"        //有几颗星        android:numStars="5"        //默认选择了几颗星,可以为小数        android:rating="4"        //不能进行选择,用来表示是一个指示器        android:isIndicator="true"        />事件处理:        ratingBar= (RatingBar) findViewById(R.id.ratingBar);        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {            @Override            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {                if(fromUser){                    Toast.makeText(MainActivity.this,"rating="+rating,Toast.LENGTH_SHORT).show();                }            }        });
spinner(类似于下拉列表的功能)xml方式:1.在string.xml中添加数据    <array name="city">        <item>北京</item>        <item>上海</item>        <item>天津</item>    </array>2.在组件中引用:  <Spinner        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/spinner"        android:layout_centerVertical="true"        android:layout_alignParentStart="true"        //引用数据        android:entries="@array/city"        />代码方法添加数据        spinner = (Spinner) findViewById(R.id.spinner2);        String[] roles = {"管理员","VIP","游客"};        /** 创建一个数组适配器         *  参数:         *      1.上下文,         *      2.下拉列表里的布局         *      3.显示下拉选项的组件id         *      4.数据         *         */        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,android.R.id.text1,roles);        spinner.setAdapter(adapter);
autoCompleteTextView(输入时显示匹配的下拉列表)

progressBar(进度条):
这里写图片描述

   android:max="100"        android:progress="50"        //不确定的进度,会一直走        android:indeterminate="true"
progressDialog(对话框时度条)    public void  showDialogProgress(View v){        ProgressDialog pd = new ProgressDialog(this);        pd.setMax(100);        pd.setProgress(30);        pd.setTitle("下载框");        pd.setMessage("下载中……");        pd.show();    }------------------------------------------------------------------ //设置标题栏进度条        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        setContentView(R.layout.activity_main);        //显示进度条        setProgressBarVisibility(true);自定度进度条:1.在res/drawable/下创建一个layer-list2.设置ProgressBar 的 android:indeterminateDrawable属性<layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item>        <rotate android:drawable="@drawable/a"            //从几度开始            android:fromDegrees="0"            //几度结束            android:toDegrees="360"            //中心坐标            android:pivotX="50%"            android:pivotY="50%"            ></rotate>    </item></layer-list>引用xml文件:android:indeterminateDrawable="@drawable/process_bg"
0 0
原创粉丝点击