Android开发基础之复习笔记

1,toast的使用

Toast.makeText(context, text, Toast.LENGTH_LONG).show();

2,toast中显示图片及文字的使用方法

代码如下:

private void midToast(String str, int showTime)
{
Toast toast = Toast.makeText(mContext, str, showTime);
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0); //设置显示位置
LinearLayout layout = (LinearLayout) toast.getView();
layout.setBackgroundColor(Color.BLUE);
ImageView image = new ImageView(this);
image.setImageResource(R.mipmap.ic_icon_qitao);
layout.addView(image, 0);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.YELLOW); //设置字体颜色
toast.show();
}

3,线性布局(两种布局方式及嵌套布局)

水平布局

android:orientation="horizontal"

垂直布局

android:orientation="vertical"

示例代码如下:

<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" >

<!-- 性别 -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:   "
            android:textSize="20sp" />

        <RadioGroup
            android:id="@+id/rg_sex"
            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:layout_marginRight="60dp"
                android:layout_weight="1"
                android:text="男"
                android:textSize="20sp" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"
                android:textSize="20sp" />
        </RadioGroup>
    </LinearLayout>

<!-- 兴趣 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="兴趣:   "
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_run"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="运动"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_games"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="游戏"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_study"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学习"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="音乐"
            android:textSize="20sp" />
    </LinearLayout>
 </LinearLayout>

4,相对布局(四个定位位置属性,八个对齐的属性)

 

5,Button的使用及后台绑定事件(两种方法),点击修改背景色,松开还原背景色

单击事件 onClick事件

  • 按下
  • 松开
  • 拖动
@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// Toast.makeText(getApplicationContext(), "down", 0).show();
			// 改变按钮的颜色
			btnToast.setBackgroundColor(Color.DKGRAY);
			//1:获取当前鼠标或者手指点击的坐标位置
		}

		if (event.getAction() == MotionEvent.ACTION_UP) {
			// Toast.makeText(getApplicationContext(), "up", 0).show();
			btnToast.setBackgroundColor(Color.GRAY);
		}

		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			tvShowMessage.setText(tvShowMessage.getText() + "move,");
		}
		return false;
	}

6,ImageButton 的使用,图片布局,点击修改背景图等.

设置ImageButton按钮,点击后切换到另外一张图片,比如2张图片A,B,初始默认为A,点击完成后切换成B,再次点击,切换成A,这样的效果。

public class MainActivity extends Activity {
			boolean is = true;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ImageButton btn = (ImageButton) findViewById(R.id.imageButton1);
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (is) {
					((ImageButton) v).setImageDrawable(getResources()
							.getDrawable(R.drawable.on));
					is = false;
				} else {
			((ImageButton) v).setImageDrawable(getResources()
							.getDrawable(R.drawable.off));
					is = true
				}
				
		});
}

7,ImageView的使用,全屏显示,子线程等待之后跳转页面

  • src属性和background属性的区别:

①background通常指的都是背景,而src指的是内容
②当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸
而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸

  • 全屏显示: 
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //无title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN); //全屏
setContentView(R.layout.main); 
  • 在Manifest文件中修改

在默认启动的Activity里添加

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

 

  • 延时跳转
        // 延时跳转
		Thread thread = new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(5000);//延时5秒
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// 界面跳转
				Intent intent = new Intent(MainActivity.this,
						NextActivity.class);
				startActivity(intent);
				// 销毁跳转之前的界面
				finish();
			}
		});
		thread.start();
	}

8,页面的跳转及传值,以及值得显示

Intent it = new Intent(xx,xx);
	Bundle bd = new Bundle();
	bd.putInt("num",1);
	bd.putString("detail","呵呵");
	it.putExtras(bd);
	startActivity(it);
	
	Intent it = getIntent();
	Bundle bd =it.getExtras();
	int number = bd.getInt("num");
	String detail = bd.getString("detail");

 

9,下拉菜单spinner的使用,设定值,及点击选中事件

 

 

示例代码如下:

Spinner sp_address; // 初始化下拉菜单
List<String> addressList;// 初始化一个数组用来存储地址
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sp_address = (Spinner) findViewById(R.id.sp_address);
		addressList = new ArrayList<String>();
		addressList.add("北京");
		addressList.add("上海");
		addressList.add("广州");
		addressList.add("深圳");
		addressList.add("郑州");
		addressList.add("南阳");
		addressList.add("内蒙");
		// 适配器
		ArrayAdapter adapter = new ArrayAdapter<String>(this,
		 android.R.layout.simple_spinner_item, addressList);
		// 设置样式
	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 加载适配器
		sp_address.setAdapter(adapter);
	}
	public void onClick(View v) {
			// 拿到下拉菜单的去向内容
			String address = sp_address.getSelectedItem().toString();

10,CheckBox的使用及点击选中事件

选中一个复选框就会吐司出相应的内容

选中多个多选框就会吐司出多个内容

取消选中时吐司出取消内容

布局代码:

   <!-- 兴趣 -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="兴趣:   " />

        <CheckBox
            android:id="@+id/cb_game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="游戏" />

        <CheckBox
            android:id="@+id/cb_study"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学习" />

        <CheckBox
            android:id="@+id/cb_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="音乐" />
    </LinearLayout>

MainActivity.java代码如下:

	private CheckBox cb_game;
	private CheckBox cb_study;
	private CheckBox cb_music;
                cb_game = (CheckBox) findViewById(R.id.cb_game);// 游戏
		cb_study = (CheckBox) findViewById(R.id.cb_study);// 学习
		cb_music = (CheckBox) findViewById(R.id.cb_music);// 音乐
	
        cb_game .setOnCheckedChangeListener(this);
        cb_study .setOnCheckedChangeListener(this);
        cb_music .setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
       if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),0).show();
    }

    @Override
    public void onClick(View view) {
        String interest= "";
                     if (cb_game.isChecked()) {
				interest += cb_game.getText().toString() + " ";
				Toast.makeText(MainActivity.this, interest, 0).show();
			} else {
				Toast.makeText(MainActivity.this, "你取消了" + interest, 0).show();
			}
			if (cb_study.isChecked()) {
				interest += cb_study.getText().toString() + " ";
				Toast.makeText(MainActivity.this,interest,0).show();
			}else {
				Toast.makeText(MainActivity.this,"你取消了"+interest,0).show();
			}
			if (cb_music.isChecked()) {
				interest += cb_music.getText().toString() + " ";
				Toast.makeText(MainActivity.this,interest,0).show();
			}else {
				Toast.makeText(MainActivity.this,"你取消了"+interest,0).show();
			}
}

11,RadioGroup及RadioButton 的使用,单选功能,默认选中功能,对应的选中点击事件

布局代码:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别"
        />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/btnMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"/>//默认选中

        <RadioButton
            android:id="@+id/btnWoman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>

MainActivity.java代码

RadioGroup userSex = (RadioGroup) findViewById(R.id.rg_sex);// 性别	
                // 性别单选
		userSex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				String sex = "";
				for (int i = 0; i < userSex.getChildCount(); i++) {
					RadioButton rb = (RadioButton) userSex.getChildAt(i);
					if (rb.isChecked()) {
						sex = rb.getText().toString();
						Toast.makeText(getApplicationContext(), sex, 0).show();
					}
				}
			}
		});

12,TextView的使用及获取值,设定值

 <TextView
        android:id="@+id/txtOne"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="center"            //位置
        android:text="TextView(显示框)"     //文本框显示内容
        android:textColor="#EA5246"         //字体颜色
        android:textStyle="bold|italic"     //字体样式
        android:background="#000000"        //背景颜色
        android:textSize="18sp" />          //字体大小
  • id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id!
  • layout_width:组件的宽度,一般写:"wrap_content"或者"match_parent"前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器。也可以自定义大小,单位是"dp"。
  • layout_height:组件的高度,内容同上。
  • gravity:设置控件中内容的对齐方向。
  • text:设置显示的文本内容。
  • textColor:设置字体颜色。
  • textStyle:设置字体风格,三个可选:"normal"(无效果),"bold"(加粗),"italic""(斜体)
  • textSize:字体大小,单位一般是用sp。
  • background:控件的背景颜色,可以理解为填充整个控件的颜色,也可以是图片

13,EditText 的使用及获取值,设定单行,跑马灯,最大长度,最大显示文字字符,密码框,只能输入数字,小数等

 

布局代码:

  <EditText
            android:id="@+id/et_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户账号"
            android:inputType="text"
            android:minWidth="150dp" />
<EditText
            android:id="@+id/et_pwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:minWidth="150dp" />
  • EditText默认是多行显示的,并且能够自动换行,即当一行显示不完的时候,他会自动换到第二行
android:singleLine="true"  //只允许单行输入
android:minLines="3"       //设置最小行的行数
android:maxLines="3"       //设置EditText最大的行数
  • 常用数值类型
android:inputType="number"  
android:inputType="phone"//拨号键盘  
android:inputType="datetime"  
android:inputType="date"//日期键盘  
android:inputType="time"//时间键盘
  • 常用文本类型,多为大写、小写和数字符号
android:inputType="none"  
android:inputType="text"  
android:inputType="textPassword"
  • 跑马灯
  android:ellipsize="marquee"    //表示是跑马灯显示
   android:focusable="true"       //要显示该跑马灯,view必须要获得焦点,只有在取得焦点的情况下跑马灯才会显示    
   android:focusableInTouchMode="true"   //通过touch来获得focus
  android:marqueeRepeatLimit="marquee_forever"   //表示滚动回数,这里这么设置,表示一直滚动
  android:maxEms="6"        //设置TextView的宽度为最长为N个字符的宽度
  android:scrollHorizontally="true"    //设置文本超出TextView的宽度的情况下,是否出现横拉条。
  android:singleLine="true"     //单行显示

14,页面修改标题,有两种方式

第一种:

setTitle("ssss");//里面输入要修改的名字

第二种:

//通过AndroidManifest.xml来进行修改
android:label="@string/app_name" 

15,EditText密码框使用CheckBox控制是否显示密码

et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
	et.setSelection(et.getText().length());//获取光标位置
					
	et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
	et.setSelection(et.getText().length());//获取光标位置

 

16,AlertDialog的使用

AlertDialog 的构造方法全部是 Protected 的,所以不能直接通过 new 一个 AlertDialog 来创建出一个 AlertDialog。

要创建一个 AlertDialog,就要用到 AlertDialog.Builder 中的 create() 方法。

  • setTitle :为对话框设置标题
  • setIcon :为对话框设置图标
  • setMessage:为对话框设置内容
  • setView : 给对话框设置自定义样式
  • setItems :设置对话框要显示的一个 list,一般用于显示几个命令时
  • setMultiChoiceItems :用来设置对话框显示一系列的复选框
  • setNeutralButton :普通按钮
  • setPositiveButton :给对话框添加 "Yes" 按钮
  • setNegativeButton :对话框添加 "No" 按钮
  • create : 创建对话框
  • show :显示对话框

带按钮的 AlertDialog

MainActivity.java代码:

 public void onCreate(Bundle savedInstanceState) {   
 super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
 
        Dialog alertDialog = new AlertDialog.Builder(this).   
                setTitle("确定删除?").   
                setMessage("您确定删除该条信息吗?").   
                setIcon(R.drawable.ic_launcher).   
                setPositiveButton("确定", new DialogInterface.OnClickListener() {   
 @Override 
 public void onClick(DialogInterface dialog, int which) {   
 // TODO Auto-generated method stub   
                    }   
                }).   
                setNegativeButton("取消", new DialogInterface.OnClickListener() {   
 @Override 
 public void onClick(DialogInterface dialog, int which) {   
 // TODO Auto-generated method stub   
                    }   
                }).   
                setNeutralButton("查看详情", new DialogInterface.OnClickListener() {   
 @Override 
 public void onClick(DialogInterface dialog, int which) {   
 // TODO Auto-generated method stub   
                    }   
                }).   
                create();   
        alertDialog.show();   
}

17.去掉标题栏

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 去掉标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_main);

18.区域背景透明度

   <LinearLayout
        android:id="@+id/ll_all"
        android:layout_width="200dp"
        android:layout_height="250dp"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="80dp"
        android:background="#111"
        android:orientation="vertical" >
  </LinearLayout>

java代码

ll_all = (LinearLayout) findViewById(R.id.ll_all);
ll_all.getBackground().setAlpha(25);

19.边框

 

可以应用到多种输入框,编辑框,按钮等...

布局代码

   <!-- 圆角弧度 -->
    <corners android:radius="10dp" />

    <!-- 形状的填充色 -->
    <solid android:color="#FFFFFF" />

    <!-- 边框宽度和颜色 -->
    <stroke
        android:width="1dp"
        android:color="#1A2B38" />

20.横线

 

布局代码:

 <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#CDCDCD" />

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值