Android学习笔记之四
来源:互联网 发布:unity3d 鼠标旋转视角 编辑:程序博客网 时间:2024/06/03 19:47
基本视图介绍
1.文本 按钮与输入框
文本 按钮 输入框的继承关系
TextView:
android:text=”文本”
android:textSize=”20sp”
android:textColor=”#FF0”
android:textStyle=”bold”
android:lines=”3”
android:singleLine=”true”
android:typeface=”monospace” //设置字型。字形有:normal, sans, serif,monospace
android:clickable=””
Button:
属性与TextView基本相似。
不同点:
1. 按钮是自带了背景的控件
2. 按钮是可以点击了
EditText:
android:hint=”请输入QQ”
android:editable=”true”
android:maxLength=”50” 设置最大的字数
android:inputType=”textPassword”
2.是非选择框
ToggleButton:
android:textOn=”开启”
android:textOff=”关闭”
android:checked=”true”
RadioButton(单选框):
RadioGroup 单选组
RadioButton 单选框
一般它们是配合使用的
设置监听器的时候,通过 RadioGroup radioGroup.setOnCheckedChangeListener();
android:checked=”true”
CheckBox:
设置监听器的时候 每个CheckBox都应该设置
android:checked=”false”
附 ToggleButton和CheckBox练习代码:
MainActivity.javapackage com.m520it.select;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity{ private List<String> mChooseData = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckBox aCbx = (CheckBox) findViewById(R.id.cbx_apple); CheckBox bCbx = (CheckBox) findViewById(R.id.cbx_banance); CheckBox cCbx = (CheckBox) findViewById(R.id.cbx_orange); myCheckedChangeListener listener = new myCheckedChangeListener(); aCbx.setOnCheckedChangeListener(listener); bCbx.setOnCheckedChangeListener(listener); } class myCheckedChangeListener implements OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int CbxId = buttonView.getId(); if(CbxId==R.id.cbx_apple){ if(isChecked){ mChooseData.add("苹果"); }else{ if(mChooseData.contains("苹果")){ mChooseData.remove("苹果"); } } }else if(CbxId==R.id.cbx_banance){ if(isChecked){ mChooseData.add("香蕉"); }else{ if(mChooseData.contains("香蕉")){ mChooseData.remove("香蕉"); } } }else if(CbxId==R.id.cbx_orange){ if(isChecked){ mChooseData.add("橘子"); }else{ if(mChooseData.contains("橘子")){ mChooseData.remove("橘子"); } } } } } public void myClick(View v){ String result = ""; for (int i = 0; i < mChooseData.size(); i++) { result+=mChooseData.get(i); } Toast.makeText(this, result, Toast.LENGTH_SHORT).show(); }}布局文件activity_main.xml:<LinearLayout 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:orientation="vertical" > <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textOff="关" android:textOn="开" /> <CheckBox android:id="@+id/cbx_apple" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="苹果" /> <CheckBox android:id="@+id/cbx_banance" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="香蕉" /> <CheckBox android:id="@+id/cbx_orange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="橘子" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myClick" android:text="button" /></LinearLayout>
附 RadioGroup和RadioButton练习代码:
MainActivity.javapackage com.m520it.select;import android.app.Activity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;public class MainActivity extends Activity implements OnCheckedChangeListener { private RadioButton mARg; private RadioButton mBRg; private RadioButton mCRg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radiogroup); radioEvent(); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (R.id.rg_a == checkedId) { Toast.makeText(this, mARg.getText().toString(), Toast.LENGTH_SHORT) .show(); } else if (R.id.rg_b == checkedId) { Toast.makeText(this, mBRg.getText().toString(), Toast.LENGTH_SHORT) .show(); } else if (R.id.rg_c == checkedId) { Toast.makeText(this, mCRg.getText().toString(), Toast.LENGTH_SHORT) .show(); } } public void radioEvent() { RadioGroup rg = (RadioGroup) findViewById(R.id.rg); mARg = (RadioButton) findViewById(R.id.rg_a); mBRg = (RadioButton) findViewById(R.id.rg_b); mCRg = (RadioButton) findViewById(R.id.rg_c); rg.setOnCheckedChangeListener(this); }}布局文件R.layout.radiogroup:<?xml version="1.0" encoding="utf-8"?><RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rg" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="苹果" android:id="@+id/rg_a" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="香蕉" android:id="@+id/rg_b" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="西瓜" android:id="@+id/rg_c" /></RadioGroup>
3.进度条
没有进度的进度条
- 大进度条
style=”?android:attr/progressBarStyleLarge” - 普通进度条
style=”?android:attr/progressBarStyle” - 小进度条
style=”?android:attr/progressBarStyleSmall”
有进度的进度条
style=”?android:attr/progressBarStyleHorizontal”
android:progress=”80”
android:max=”100”
3.可拖动的进度条
SeekBar:可拖动的进度条
android:max=”100”
android:progress=”50”
4.星星进度条
RatingBar:星星进度条 基本单位为半颗星
android:numStars=”4”
android:rating=”3.5”
android:stepSize=”0.5”
练习代码:
<LinearLayout 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:orientation="vertical" > <!-- 没有进度的进度条 --> <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar style="?android:attr/progressBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 有进度的进度条 --> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:max="100" android:progress="80" /> <!-- 可以拖动的进度条 --> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="40" /> <!--星星进度条 --> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="4" android:rating="3.5" android:stepSize="0.5" /></LinearLayout>
3.图片控件(ImageView)
设置图片源
android:src=”@drawable/ic_launcher”
代码设置图片源
imageView.setImageResource(resId)
imageView.setImageBitmap(bm);
imageView.setImageDrawable(drawable);
设置缩放模式
android:scaleType=”“
练习demo:
MainActivity.javapackage com.m520it.imageView;import android.app.Activity;import android.os.Bundle;import android.widget.ImageView;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.iv2); /*imageView.setImageResource(resId) imageView.setImageBitmap(bm); imageView.setImageDrawable(drawable);*/ imageView.setImageResource(R.drawable.flower); }}布局文件activity_main.xml<LinearLayout 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:orientation="vertical" > <ImageView android:id="@+id/iv1" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop" android:src="@drawable/flower" /> <ImageView android:id="@+id/iv2" android:layout_width="150dp" android:layout_height="150dp" /></LinearLayout>
4.滑动控件
- 手机界面无法容纳更多的控件的时候,就需要滚动界面。
- ScrollView :控制上下滑动的效果
- HorizontalScrollView: 控制左右滑动的效果
- ScrollView与HorizontalScrollView只能容纳一个子控件。
HorizontalScrollView的布局demo<?xml version="1.0" encoding="utf-8"?><HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!" android:textSize="30sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!" android:textColor="#F00" android:textSize="30sp" /> </LinearLayout></HorizontalScrollView>ScrollView的布局demo<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world" android:lines="10" android:textSize="30sp" /> <TextView android:text="Hello world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#F00" android:lines="10" android:textSize="30sp" /> <TextView android:text="Hello world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#F00" android:lines="10" android:textSize="30sp" /> </LinearLayout></ScrollView>
5.日期时间选择器
直接敲一个demo就理解了
MainActivity.javapackage com.m520it.dateAndTime;import java.util.Calendar;import java.util.Locale;import android.app.Activity;import android.os.Bundle;import android.widget.DatePicker;import android.widget.DatePicker.OnDateChangedListener;import android.widget.TimePicker;import android.widget.TimePicker.OnTimeChangedListener;import android.widget.Toast;public class MainActivity extends Activity implements OnDateChangedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取日期控件 DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker); Calendar calendar = Calendar.getInstance(Locale.CHINESE); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); //init用来初始化改变后的日期 //this 日期改变的监听器 datePicker.init(year, month, day,this); //获取时间控件 TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker); //设置24小时 timePicker.setIs24HourView(true); //设置监听器 timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { System.out.println(hourOfDay+"..."+minute); Toast.makeText(MainActivity.this,hourOfDay+"..."+minute,Toast.LENGTH_SHORT).show(); } }); } @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(this,"year:"+year+" mothOfYear:"+(monthOfYear+1)+" dayOfMonth:"+dayOfMonth, Toast.LENGTH_SHORT).show(); }}布局文件activity_main.xml:<LinearLayout 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" tools:context="com.m520it.dateAndTime.MainActivity" android:orientation="vertical" > <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
- Android学习笔记之四
- android学习笔记之四TabHost布局
- android学习笔记之四TabHost布局
- Android Fragment系列学习笔记之四
- Android学习笔记四之Activity
- Android学习笔记四之Activity
- Android学习笔记四
- Android 学习笔记(四)
- Android学习笔记(四)
- android学习笔记之四:四大组件之BroadcastReceiver
- Android学习笔记(四) 之模拟发短信
- Android 学习笔记之四 View的事件分发机制
- JSP学习笔记之四
- C++学习笔记之四
- JAVA学习笔记之四
- Mysql学习笔记之四
- jQuery学习笔记之四
- javascript学习笔记之四
- 算法?效率高的计算方法?——计算机基础算法
- 2683: 简单题
- 获取输入字符串每个字符的次数
- 交叉熵代价函数
- JavaScript parseInt() 函数
- Android学习笔记之四
- vmware设置centos虚拟机nat联网
- Log4j配置详解
- [转]JQuery.Ajax之错误调试帮助信息
- 【iOS开发】在UILabel中同时显示图片和文字,"混排"。
- 一些命令记录
- 使用caffe训练时Loss变为nan的原因
- CSS权威指南阅读笔记(十)--浮动和定位
- spring mvc 发送带参数的请求报404,同一个包下其他的controller没有问题(扫描的包没有问题)