1.TextView
textview是我们常见的界面布局了,每次new一个新的project都会有他,让我们来认识认识他
这里要注意的是,有些朋友觉得android:ellipsize="end"失效,没效果就是需要设置最大行数,让末尾显示不出的内容变为省略号。其他基本的属性都比较常用,大家一定都有所了解。
2.EditText
hint是在用户未输入密码时,显示在输入框的内容。
textColorHint字面意思就是对hint字体颜色的编辑
inputtype是指输入的规范,还有的比如是,
以下是运行结果:
3.Toast类
在讲下面内容时,要先知道这个类,相当于预备知识啦,让我们来看看到底是何方神圣,Toast是信息提醒的机制,用于向用户提供即时消息。
Toast显示提供信息的示例如下:
Toast.makeText(MainActivity.this,"你来啦",Toast.LENGTH_SHORT).show();
第一个参数是应用程序环境的信息,即上下文环境,如果在Activity中就用 “当前Activity.this”。
第二个参数是一个字符串表示提示的信息。
第三个参数是提示信息的时间,一般分为2种情况 SHORT 和 LONG 分为长和短。
了解这些后就可以往下学习了。
4.Button
实现Button有三种方式,我们来一一介绍一下。
(1)第一种方式基本不用,了解一下即可
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
/>
就是添加一个onClick属性即可,后面的click需要自己在Mainactivity中实现。
(2)第二种比较常用
在Layout中:设置一个id
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
/>
在MainActivity中:先用findViewById , 再用 setOnClickListener
View btn = findViewById(R.id.btn);
//通过id设置一个View对象
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"你来啦",Toast.LENGTH_SHORT).show();
}
});
设置一个按钮,按钮的作用就是即时的提供消息。
(3)第三种方式就是最常用的,因为他可以比较方便设置多个按钮的事务。
首先在MainActivity中先实现View.OnClickListener 。
public class MainActivity extends AppCompatActivity implements View.OnClickListener
然后再onCreat方法面前定义Button变量(这里举例三个)
接下来的onClick是写按钮的功能的,如果有多个,可以使用switch来实现。
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
Toast.makeText(MainActivity.this, "按钮一被点击了",
Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Toast.makeText(MainActivity.this, "按钮二被点击了",
Toast.LENGTH_SHORT).show();
break;
case R.id.btn3:
Toast.makeText(MainActivity.this, "按钮三被点击了",
Toast.LENGTH_SHORT).show();
break;
}
}
我这里是实现了按钮点击出现消息提示的功能,供参考。
5.ImageView
图片控件比较简单,这里给大家举例子:
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="145dp"
android:background="#fff000"
android:scaleType="center"
android:src="@mipmap/ic_launcher_round"
app:tint="@color/white" />
src是图片路径、scaleType是图片的位置下界面的布局、tint是给图片渲染颜色(注意,这里要用app:tint 不要用 abdroid:tint 已经out啦hhh)
6.RadioButton
这个是单选框的按钮控件,注意这个按钮控件要被RadioGroup包裹起来,其他的属性都会常见,相信大家都认识,这里举个例子:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/boy"
android:text="男"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/girl"
android:text="女"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
接下来给大家说一下RadioButton的事件监听,其实我认为事件监听就是,android能反映出你选择的信息,这就算事件监听,比如前面的按钮Toast的消息提示就算。一般事件监听就是在MainActivity中设置
首先,在顶部声明:
private RadioGroup rad;
private TextView text;
再在OnCreat里面赋值:
text = findViewById(R.id.tvv);
rad = findViewById(R.id.rad);
注意!!!这里别在text和rad前面声明View,不然后面rad用不了setOnCheckedChangeListener
方法!!!!别问,问就是我爆红了半天才看到,5555~~~
最后,运用rad的setOnCheckedChangeListener方法,写事件监听
rad.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.boy:
text.setText("你是男滴");break;
case R.id.girl:
text.setText("你是女滴");break;
}
}
});
最后给大家看看运行结果:
7.CheckBox
最后,最后,最后一个控件啦,就是多选框控件
可以应用在选兴趣爱好,如:唱跳rap篮球(bushi)
非常简单,来看看:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择你的技能doge:"
/>
<CheckBox
android:id="@+id/skill1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="唱"/>
<CheckBox
android:id="@+id/skill2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳"/>
<CheckBox
android:id="@+id/skill3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rap"/>
预览一下结果:
我们也来看看多选框的事件监听:
为了体现事件监听,我们可以再加一个文本,显示监听结果:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="的技能doge是:"
android:textSize="30dp"
android:textColor="#F0F0F0"
/>
再Mainactivity中继承AppCompatActivity,实现CompoundButton.OnCheckedChangeListener
再oncreat前面声明
private String hobbys;
private TextView hobby;
并且写下
再写下onCheckedChanged方法
被选中的结果:
取消勾选的结果: