常用控件的使用方法
TextView
它主要用于在界面上显示一段文本信息
举个栗子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#00ff00"
android:text="This is TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</LinearLayout>
在TextView中我们使用android:id给当前空间定义一个唯一标识符。
然后使用android:layout_width和android:layout_height制定了控件的宽度和高度,这两个属性的可选值为match_parent,fill_parent和warp_content,前两个都表示让当前控件与父布局大小一样,推荐match_parent,warp_content表示让空间大小刚好包含住内容,也就是由控件内容决定大小。
通过android:gravity来指定文字的对齐方式,可选值有top,bottom,left,right,center,可以用" | "来指定多个值,比如center|left。
通过android:textSize属性可以指定文字的大小,单位为sp
通过android:textColor属性可以指定文字的颜色
通过android:text指定TextView中显示的文本内容
Button
Button是程序用于和用户进行交互的一个重要控件。他可配置的属性和TextView差不多。
举个栗子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false"
/>
</LinearLayout>
由于系统会对Button中的英文字母自动进行大写转换,如果这不是你想要的,可以配置属性android:textAllCaps,可选值为true,false,这里选择false禁用大写转换。
在MainActivity中为Button的点击事件注册一个监听器,代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
如果你不喜欢使用匿名类的方式;来注册监听器,也可以使用实现接口的方式来进行注册,代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{//实现接口
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button:
//在此处添加逻辑
break;
default:
break;
}
}
}
EditText
EditText是程序用于和用户进行交互的另一个控件,它允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理。
举个栗子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here"
android:maxLines="2"
/>
</LinearLayout>
android:hint属性指定一段提示性的文本
android:maxLines指定了EditText的最大行数为两行,这样当输入内容超过两行时,文本会向上滚动,而EditView不会继续拉伸