Android UI控件学习笔记(二)


Andrid

AndrordManinfest.xml 1.APP权限 2.打开方式

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" /> //启动

</intent-filter>

同步

JAVA程序代码 + 资源

Android UI控件

New 申请空间 在堆里 强引用

了解以下基本控件
TextView(文本控件)
EditText(可输入文本框)

android:layout_width="match_parent"//匹配父窗口

android:layout_width="wrap_content"//匹配屏幕

android:gravity="center_horizontal"//内容居中


Button(按钮)
RadioButton以及RadioGroup(单选按钮)
CheckBox(多选框)
ImageView(图片控件)

TextView的基本属性
androidtext
android : textColor
android : textSize

EditText的基本属性
androidinputType
androidhint
android:drawableLeft
android:drawablePadding


1.Activity-main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.myapplication.MainActivity">    <TextView        android:layout_width="300dp"        android:layout_height="50dp"        android:text="@string/test"        android:gravity="center"        android:textColor="#4f62f4"       android:textSize="30sp"        android:layout_alignParentTop="true"        android:layout_alignParentStart="true"        android:id="@+id/textView" /><EditText    android:textSize="25sp"    android:layout_width="match_parent"    android:layout_height="50dp"    android:textColor="#ff5044"    android:hint="请输入用户名"    android:drawableLeft="@mipmap/ic_launcher"    android:drawablePadding="20dp"    android:id="@+id/editText"    android:layout_gravity="center_horizontal"    android:layout_above="@+id/editText2"    android:layout_alignParentStart="true" />    <!-- zhangyufeng 注释--><EditText        android:textSize="25sp"    android:layout_width="match_parent"        android:layout_height="50dp"        android:hint="请输入密码"        android:maxLength="12"        android:inputType="textPassword"        android:drawableLeft="@mipmap/ic_launcher"        android:drawablePadding="20dp"        android:layout_gravity="center_horizontal"    android:id="@+id/editText2"    android:layout_centerVertical="true"    android:layout_alignParentStart="true" />    <RadioGroup        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/rg"        android:orientation="vertical"        android:layout_below="@+id/editText2"        android:layout_alignParentStart="true"        android:layout_marginTop="51dp">    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="男"        android:checked="true"        android:id="@+id/nan"        android:layout_below="@+id/editText2"        android:layout_alignParentStart="true"        />    <RadioButton    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="女"    android:id="@+id/nv"        android:layout_alignTop="@+id/nan"        android:layout_toEndOf="@+id/nan" />    </RadioGroup>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="登陆"        android:id="@+id/btLogin"        android:layout_alignParentBottom="true"        android:layout_toEndOf="@+id/rg" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="取消"        android:id="@+id/btLogin2"        android:layout_alignParentBottom="true"        android:layout_alignEnd="@+id/textView" />    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="运动"        android:id="@+id/ch01"        android:checked="false"        android:layout_alignTop="@+id/rg"        android:layout_alignStart="@+id/btLogin" />    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="音乐"        android:id="@+id/ch02"        android:checked="false"        android:layout_below="@+id/ch01"        android:layout_alignStart="@+id/ch01" />    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Android"        android:id="@+id/ch03"        android:layout_below="@+id/rg"        android:layout_alignStart="@+id/btLogin"        android:checked="true" />    <ImageView        android:layout_width="match_parent"        android:layout_height="200dp"        android:src="@mipmap/zyf"        android:layout_below="@+id/textView"        android:layout_above="@+id/editText"        android:layout_alignEnd="@+id/textView"        /><!-- android:scaleType="centerCrop"图片平铺--></RelativeLayout>


2.Activity-main2.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"><TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="@string/test"    android:textSize="25sp"    android:textColor="#ff8543"    /><EditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="请输入用户名"    android:drawableLeft="@mipmap/ic_launcher"    /><EditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="请输入密码"    android:inputType="textPassword"    android:drawableLeft="@mipmap/ic_launcher"    /><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="登陆"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="取消"        /></LinearLayout><LinearLayout    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal"    >    <RadioGroup        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">         <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="男"        android:checked="true"        android:id="@+id/nan"/>        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="女"            android:checked="true"            android:id="@+id/nv"            />    </RadioGroup></LinearLayout></LinearLayout>


3.Activity-main3.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="用户名"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入用户名"        android:id="@+id/user"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="密码"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入密码"        android:id="@+id/psw"        android:inputType="textPassword"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="请再次输入密码"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="textPassword"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical"            android:layout_weight="1"           >            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="性别"                />        <RadioGroup            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:id="@+id/rg">            <RadioButton                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="男"                android:checked="true"                android:id="@+id/nan"/>            <RadioButton                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="女"                android:id="@+id/nv"/>        </RadioGroup>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我的技能有"        />    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="JAVA"        android:id="@+id/ch01"        android:checked="true"        />    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="IOS"        />    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Android"        android:checked="true"        />        </LinearLayout>    <LinearLayout    android:layout_width="wrap_content"    android:layout_height="wrap_content"        android:layout_weight="1"       >        <ImageView        android:layout_width="200dp"        android:layout_height="200dp"            android:id="@+id/ttpp"/>    </LinearLayout></LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal">        <Button            android:layout_width="300dp"            android:layout_height="wrap_content"            android:gravity="center"            android:text="注册"            android:id="@+id/regt"            />    </LinearLayout></LinearLayout>
3.MainActivity

package com.example.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.ImageView;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends AppCompatActivity {   private RadioGroup rg;    private Button bt;private ImageView iv;    private CheckBox cb;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //为Activity设计布局        setContentView(R.layout.activity_main3);        //从布局中通过ID找到Button控件       bt=(Button) findViewById(R.id.regt);       final EditText user= (EditText) findViewById(R.id.user);      rg= (RadioGroup) findViewById(R.id.rg);        iv= (ImageView) findViewById(R.id.ttpp);        cb= (CheckBox) findViewById(R.id.ch01);        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if(isChecked){                    Toast.makeText(getBaseContext(),"你选择了"+cb.getText(),                            Toast.LENGTH_SHORT).show();                }            }        });        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {          @Override          public void onCheckedChanged(RadioGroup group, int checkedId) {              //判断男女根据所选ID//              if (checkedId==R.id.nan){//                  Toast.makeText(getBaseContext(),"男",Toast.LENGTH_SHORT).show();//              }else{//                  Toast.makeText(getBaseContext(),"女",Toast.LENGTH_SHORT).show();//              }              if (checkedId==R.id.nan){                 iv.setImageResource(R.mipmap.zz);             }else{                  iv.setImageResource(R.mipmap.zyf);              }          }      });                //为按钮设计一个点击监听事件        bt.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this, user.getText(), Toast.LENGTH_SHORT).show();            }        });    }}



  

更多相关文章

  1. linearLayout 和 relativeLayout的属性区别
  2. Android按钮控件之RadioGroup和RadioButton
  3. Android学习笔记3之基本组件API
  4. Android界面——LinearLayout和RelativeLayout 属性对比
  5. 安卓布局属性代码中文注解
  6. 【Android常用控件】EditText常用属性【二】:为文本输入框指定软
  7. android 设置Button或者ImageButton的背景透明
  8. Android--控件属性汇总
  9. android:gravity ,和 android:layout_gravity

随机推荐

  1. View那些事儿(1) -- View绘制的整体流程
  2. 自定义View基础:Android中的颜色和颜色混
  3. Android中遍历View的几种方式
  4. Android的文字渲染
  5. Android(安卓)不得不说的VideoView的一些
  6. 彻底搞清楚Android中的 Attr
  7. UE高级性能剖析技术(三)-- Android内存分布
  8. Android(安卓)合并AAR踩坑之旅
  9. Android(安卓)OpenGL ES 从入门到精通系
  10. 在android里做一个竖着的seekbar