一 案例描述
1、考核知识点
网格布局
线性布局
样式和主题
Toast
2、 练习目标
掌握网格布局的使用
掌握Toast的使用
掌握线性布局的使用
3、 需求分析
网格布局是Android4.0新增的布局,它实现了控件的交错显示,能够避免因布局嵌套对设备性能的影响,更利于自由布局的开发。Toast是用来弹出提示信息的,接下来将综合应用网格布局、线性布局、Toast实现案例——智能输入法。
4、 设计思路(实现原理)
1) 利用网格布局编写出计算机的布局,并为“发送”按钮注册点击监听方法
2) 当点击“发送”按钮时,在监听方法click里弹出Toast显示按钮被点击
二、案例实现
(1)“智能输入法”程序
创建“智能输入法”程序,该程序的界面对应的布局文件如下所示:
<LinearLayout 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:gravity="bottom"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#22000000"
android:orientation="horizontal" >
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="3"
android:background="@drawable/login_edit_pressed" />
<Button
android:layout_weight="1"
android:background="@drawable/soil_backgroud_content3"
android:onClick="click"
android:text="发送"
android:textColor="@android:color/white" />
</LinearLayout>
android:layout_height="wrap_content"
android:background="#22000000"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<GridLayout
android:layout_gravity="bottom"
android:layout_weight="1.5"
android:columnCount="1" >
<Button android:text="," />
<Button android:text="?" />
<Button android:text="!" />
<Button android:text="符" />
</GridLayout>
android:layout_weight="4.5"
android:columnCount="3"
android:orientation="horizontal" >
<Button android:text="1" />
<Button android:text="2" />
<Button android:text="3" />
<Button android:text="4" />
<Button android:text="5" />
<Button android:text="6" />
<Button android:text="7" />
<Button android:text="8" />
<Button android:text="9" />
<Button android:text="123" />
<Button android:text="﹂ " />
<Button android:text="中/英" />
<Button android:text="清空" />
<Button android:text="0" />
<Button android:text="," />
<Button android:text="换行" />
(2)编写与界面交互的逻辑代码
在MainActivity中编写与界面交互的逻辑代码,具体如下所示:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
Toast.makeText(this, "发送文字", 0).show;
(3)运行程序
运行“智能输入法”程序,能看到如图2-2左侧所示的界面,点击“发送”按钮可以看到图2-2右侧所示的界面。
图2-2 智能输入法运行界面
三、案例总结
1、网格布局是Android4.0以后新推出的布局,因此最低兼容版本必须API14或以上。
2、案例使用到了线性布局的android:layout_weight属性,该属性是用于指定该线性布局下的子View的宽高比值。
本教学案例由传智播客高教产品研发部出品,转载请写明出处
老师领资源
关注微信教学好助手(boxuegu)
回复以下关键词领取此程序代码
教程9+姓名+电话+邮箱+学校+所教学科
例(教程9+张xx+13388xxx+123xxx@qq.com+XX大学+java学科)
(不按格式回复,一律不提供)