TextView
- 基本的
直接写
调用
- 显示不下使用…
android:text=“加油加油加油”
android:ellipsize=“end”
- 文字+icon
android:drawableRight="@地址"
- 中划线、下划线
mTv4.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);//中划线
mTv4.getPaint().setAntiAlias(true);//去除锯齿
mTv5=(TextView)findViewById(R.id.tv_5);
mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
//下划线
mTv6=(TextView)findViewById(R.id.tv_6);
mTv6.setText(Html.fromHtml(“加油加油”));
- 跑马灯
android:text=“加油加油加油加油加油加油加油加油加油加油加油加油”
android:singleLine=“true”
android:textColor="#000000"
android:textSize=“24dp”
android:ellipsize=“marquee”
android:marqueeRepeatLimit=“marquee_forever”
android:focusable=“true”
android:focusableInTouchMode=“true”
EditText
继承自TextView,可以进行编辑操作,将用户信息传递给Android程序,还可以设置监听器,用来测试用户输入的内容是否合法。
android:hint=“xxx” 输入框提示
android:maxLines=“xxx” 输入框最大行数
- 常用属性
- 监听事件
- 制作登陆界面
package com.example.zzm.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EditTextActivity extends ActionBarActivity {
private Button mBtnLogin;
private EditText mEtUserName;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
mBtnLogin=(Button)findViewById(R.id.btn_login);
mEtUserName=(EditText)findViewById(R.id.et_1);
mBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(EditTextActivity.this,"登录成功!",Toast.LENGTH_SHORT).show();
}
});
mEtUserName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.d("edittext", toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
<?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">
<EditText
android:id="@+id/ed_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FF9900"
android:hint="用户名"
android:maxLines="1"
/>
<EditText
android:id="@+id/ed_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FF9900"
android:hint="密码"
android:layout_below="@+id/ed_1"
android:layout_marginTop="15dp"
android:inputType="textPassword"
android:maxLines="1"
/>
<!-- hint 表示提示 inputType 输入形式-->
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/ed_2"
android:layout_marginTop="40dp"
android:text="登录"
android:textColor="#fff"
android:textSize="25dp"
android:background="@drawable/bg_btn3"
/>
</RelativeLayout>
Button
- 文字大小、颜色
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#0066FF"
android:background="#FF0000"
android:text="按钮1"/>
- 自定义背景形状
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--背景-->
<solid
android:color="#FF9900"/>
<!--圆角-->
<corners
android:radius="10dp"/>
<!--描边-->
<stroke
android:width="1dp"
android:color="#000000"/>
</shape>
- 自定义按压效果
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--触摸变色的感觉-->
<!--按压-->
<item android:state_pressed="true" >
<shape>
<solid android:color="#FF9900"/>
<corners android:radius="5dp"/>
</shape>
</item>
<!--不按压-->
<item android:state_pressed="false">
<shape>
<solid android:color="#00000"/>
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
点击事件
- 点击事件的第一种形式
package com.example.zzm.button;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button myBtn_one;
private Button myBtn_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到按钮控件
myBtn_one = (Button) findViewById(R.id.btn_one);
myBtn_two = (Button) findViewById(R.id.btn_two);
}
//按钮的第一种点击事件
public void click(View v){
myBtn_one.setText("按钮1已被点击");
}
}
- 点击事件的第二种形式(通过匿名内部类)
package com.example.zzm.button;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button myBtn_one;
private Button myBtn_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到按钮控件
myBtn_one = (Button) findViewById(R.id.btn_one);
myBtn_two = (Button) findViewById(R.id.btn_two);
//按钮的第二种点击事件的写法
myBtn_one.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
myBtn_one.setText("按钮1已被点击");
}
});
myBtn_two.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
myBtn_two.setText("按钮2已被点击");
}
});
}
// //按钮的第一种点击事件
// public void click(View v){
// myBtn_one.setText("按钮1已被点击");
// }
}
- 点击事件的第三种形式
package com.example.zzm.button;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private Button myBtn_one;
private Button myBtn_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到按钮控件
myBtn_one = (Button) findViewById(R.id.btn_one);
myBtn_two = (Button) findViewById(R.id.btn_two);
// //按钮的第二种点击事件的写法
// myBtn_one.setOnClickListener(new View.OnClickListener(){
// @Override
// public void onClick(View v) {
// myBtn_one.setText("按钮1已被点击");
// }
// });
// myBtn_two.setOnClickListener(new View.OnClickListener(){
// @Override
// public void onClick(View v) {
// myBtn_two.setText("按钮2已被点击");
// }
// });
//按钮的第三种点击事件
myBtn_one.setOnClickListener(this);
myBtn_two.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//具体判断是哪一个按钮
switch (v.getId()){
case R.id.btn_one:
myBtn_one.setText("按钮1已被点击");
break;
case R.id.btn_two:
myBtn_one.setText("按钮2已被点击");
break;
}
}
// //按钮的第一种点击事件
// public void click(View v){
// myBtn_one.setText("按钮1已被点击");
// }
}
RadioButton
- 常用属性
- 自定义样式
- 监听事件
<?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.zzm.radiobutton.MainActivity">
<RadioGroup
android:id="@+id/rdg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/rbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="男"
android:textSize="25dp"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="女"
android:textSize="25dp"/>
</RadioGroup>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rdg"
android:text="aaa"
android:textSize="30dp"
/>
</RelativeLayout>
package com.example.zzm.radiobutton;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.找到相关控件
final TextView textView=(TextView)findViewById(R.id.tv);
RadioGroup radioGroup=(RadioGroup)findViewById(R.id.rdg);
//2.通过radiogroup来判断是男还是女
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i==R.id.rbtn){
textView.setText("你选择是男");
}else{
textView.setText("你选择是女");
}
}
});
}
}
复选框CheckBox
- 常用属性
- 自定义样式
- 监听事件
package com.example.zzm.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class CheckBoxActivity extends ActionBarActivity {
private CheckBox mCb1,mCb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
mCb1=(CheckBox)findViewById(R.id.cb_1);
mCb2=(CheckBox)findViewById(R.id.cb_2);
mCb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this,isChecked?"1选择":"1未选择",Toast.LENGTH_SHORT).show();
}
});
mCb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this,isChecked?"2选择":"2未选择",Toast.LENGTH_SHORT).show();
}
});
}
}
<?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">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你会哪些移动开发:"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginBottom="10dp"
/>
<CheckBox
android:id="@+id/cb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:button="@drawable/bg_checkbox"
android:textSize="20sp"
android:layout_below="@+id/tv_title"/>
<CheckBox
android:id="@+id/cb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android1"
android:button="@drawable/bg_checkbox"
android:textSize="20sp"
android:layout_below="@+id/cb_1"/>
</RelativeLayout>
ImageView
- fitXY:撑满控件,宽高比可能发生改变
- fitCenter:保持宽高比缩放,直至能够完全显示
- centerCrop:保持宽高比缩放,直至完全覆盖控件,裁剪显示。
加载网络图片
package com.example.zzm.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class ImageViewActivity extends ActionBarActivity {
private ImageView mIv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
mIv1=(ImageView)findViewById(R.id.iv_1);
Glide.with(this).load("图片url").into(mIv1);
}
}
<?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">
<!--加载网络图片-->
<ImageView
android:id="@+id/iv_1"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#FF9900"
android:scaleType="fitXY"
android:layout_marginTop="10dp"
/>
<!--把图片撑满,拉伸-->
</RelativeLayout>
记住在AndroidManifest.xml声明访问网络权限