常用控件
TextView
布局xml代码中的常用属性
textColor
textStyle:bold(粗体)/normal/italic(斜体)
textSize
tyepface(字体)
//插入自定义字体
public class MainActivity extends AppCompatActivity {
TextView tvHello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_layout);
tvHello = findViewById(R.id.tv_hello);//使用id找到view
tvHello.setText("你好");
tvHello.setTypeface(Typeface.createFromAsset(getAssets(), "STXINGKA.TTF"));
}
}
textAllCaps(全部大写)
singleLine(等同于android:maxLines=1)
ellipsize:end/start/none/marquee(全显示不出的时候,选择显示哪部分,剩下部分为省略号,可实现跑马灯效果)
maxLength(限制字的个数,包括汉字字母数字都一样)
maxEms(限制文本的宽度,与个数无关)
maxLines(换行符:\n)
Java代码中的基本操作
获取文字:getText()
设置文字:setText(“hello”)
设置文字的颜色、大小等 setTextSize() setTextColor()
EditText
可由用户输入文字
布局xml代码中的常用属性
1.与TextView相同(EditView继承自TextView)
2.Android:drawableLeft/Right/Top/Bottom
在输入框上下左右设置图标(输入框右上角的叉号)
3…Android:hint
输入提示(类似于输入账号时下面灰色”输入账号“字样)
java代码中的基本操作
获取文字:getText()
设置文字:setText(“hello”)
设置光标到文字末尾:setSelection()
Button
布局xml代码中的常用属性
1.继承自TextView,具有TextView的一切属性
2.android:background
修改按钮背景
3.android:onClick
点击事件(即点击以后会发生什么)
java代码中的基本操作
获取文字:getText()
设置文字:setText(“hello”)
设置背景:setBackgroundColor()、setBackgroundResource()、setBackgroundDrawable()
设置点击事件的监听:setOnClickListener
ImageView
布局xml代码中的常用属性
1.anrioid:src
2.anrioid:scaleType
3.anrioid:background
4.anrioid:onClick
java代码中的基本操作
1.设置图片:setImageResource(R.drawable…)、setImageDrawable()、setImageBitmap()
2.设置背景:setBackgroundColor()、setBackgroundResource()、setBackgroundDrawable()
3.设置点击事件的监听:setOnClickListener
其他
1.CheckBox 勾选框
2.RadioButton 选框
所有控件共有的属性:空寂俺的可见性
xml中设置:android:Visibility
java中:setVisibility