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的基本属性
android:text
android : textColor
android : textSize
EditText的基本属性
android:inputType
android:hint
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(); } }); } }