Android中的基本控件
在使用Android开发App的过程中,我们会对APP的每一个页面进行排版,而排版的前提就是Android中一些为我们定义并且设置了固定样式的基本控件和高级控件。
<TextView>:故名知意,文本视图,用来显示文字。
<TextView
<EditText>:文本编辑框 有点类似前端中的input标签
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"//设置编辑框中可以输入的文本类型
hint //设置文本编辑框默认显示的文本
可选的值有number,numberPassword等
android:maxLength="5":控制输入框中文本的最大长度
/>
<ImageView> 图片视图 类似前端的《image》标签
<ImageView
android:layout_width="500dp"
android:layout_height="500dp"
android:src="@mipmap/jiaju"
android:scaleType="centerCrop"
/>
src 图片路径
scaleType 缩放类型
<!--scaleType:默认fitCenter
centerCrop:居中裁剪
center:按原比例居中显示
-->
<Button>:按钮
<Button android:onClick="showInfo"// 设置按钮的点击事件 android:id="@+id/personInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="个人信息"/>
android:layout_width="match_parent"
android:id="@+id/myGroup"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/rb1"android:text="男"android:layout_width="match_parent"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rb2"android:text="女"android:layout_width="match_parent"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rb3"android:text="其他"android:layout_width="match_parent"android:layout_height="wrap_content" /></RadioGroup>
<LinearLayoutandroid:id="@+id/check_layout"android:layout_width="match_parent"android:layout_height="wrap_content"><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="看书"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="打球"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="书法"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="收藏"/></LinearLayout>在获取每个checkBox并判断他们是否选中的时候我们可以这样:
public class MainActivity extends AppCompatActivity {
private LinearLayout check_layout; //外面的父容器
private Button btn_submit; //绑定的点击按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
check_layout= (LinearLayout) findViewById(R.id.check_layout);
btn_submit= (Button) findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int childCount=check_layout.getChildCount();
StringBuffer buffer=new StringBuffer();
for (int i=0;i<childCount;i++){
CheckBox checkBox= (CheckBox) check_layout.getChildAt(i);
if (checkBox.isChecked()){
buffer.append(checkBox.getText()+"\n");
}
}
Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}