一、简答题

1.  一个Android应用中的src目录,res目录下的drawable-xdpi、layout文件夹、values文件夹,R.java文件,AndroidManifest.xml文件的作用分别是什么?

答:

src目录:包含项目的所有包和源文件(.Java)

res目录下的drawable-xdpi:保存图片资源和多种XML文件

layout文件夹:保存布局资源

values文件夹:存放一些资源文件的信息

R.java文件:定义Android程序中所有各类型的资源的索引。

AndroidManifest.xml文件:整个Android应用的全局描述文件。

2.  常用的布局管理器有哪些,简述各布局管理器的布局特点。

线性布局:将容器里的组件一个挨一个排列起来,可以控制组件横向和纵向排列,并且不会换行。

相对布局:容器内的组件的位置总是相对兄弟组件、父容器来决定的。

帧布局:为每个加入其中的组件创建一个空白的区域,类似与AWT中的CardLayout,把组件叠加起来,但是不可以把下面的移到上面来。

表格布局:采用行、列的格式来管理UI组件,不需要声明包含多少行、多少列,而是通过添加TableRow、其他组件来控制行数和列数。

绝对布局:开安人员通过X坐标和Y坐标来控制组件的位置。

3.  两种事件处理机制的特点及区别。

特点:

基于监听的事件处理:是一种“面向对象”的事件处理,主要涉及事件源、事件、事件监听器。

基于回调的事件处理:重写Android组件特定的回调方法,或者重写Activity的方法。

区别:

基于监听的事件模型分工更明确,事件源、事件监听器由两个类分开实现,因此具有更好的可维护性。

Android的事件处理机制保证基于监听的事件监听器会被优先触发。

4.  简述ContentProvider的作用和使用方法。

作用:提供了不同应用程序之间交换数据的标准API。

使用方法:

1.  定义ContentProvider类,该类需要继承ContentProvider基类。

2.  在AndroidManifest.xml文件中配置该ContentProvider。

5.  请简述Android平台的优缺点。

优点:

开放性

平等性

应用无界性

快速方便的开发应用

代码和布局分离

缺点:

安全和隐私安全

系统优化不成熟

运营商仍然能够影响到手机

6.  简述SharedPreferences的作用、使用方法、特点。

作用:

保存简单类型key-value对的数据

使用方法:

1.  创建其他程序对应的Context

2.  调用其他应用程序的Context的getSharedPreferences(String name,intmode)即可获取相应的SharedPreferences对象

3.  如果需要向其他应用的SharedPreferences数据写入数据,调用SharedPreferences的edit()方法获取相应的Editor即可。

特点:

SharedPreferences是一个接口,程序无法直接创建其实例,只能通过Context提供的getSharedPreferences(Stringname,int mode) 方法来获取SharedPreferences实例。

7.  简述两个Activity之间怎么传递数据?

使用Bundle在Activity之间交换数据

Intent提供了多个重载的方法来携带额外的数据

putExtras(Bundle data):向Intent中放入需要携带的数据

方法中的Bundle是数据携带包,提供了多个方法存入数据

putXxx(String key,Xxx data):向Bundle中放入各种类型数据

putSerializable(Stringkey,Serializable data):向Bundle中放入一个可序列化的对象

为了取出Bundle数据携带包里的数据,Bundle提供如下的方法

getXxx(String key):从Bundle取出Int、long等各数据类型数据

getSerializable(String key,Serializabledata):从Bundle取出一个可序列化的对象

二、编程题(请注意参考书上相关知识点的例子)

1、按照要求,采用相对布局,实现如下界面,请写出布局文件(xml)的代码。


<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

 

    <TextView

        android:id="@+id/label"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Typehere:"

        android:textColor="#ffff0000"

        android:textSize="30dp"/>

 

 

    <EditText

        android:id="@+id/entry"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/label"/>

 

    <Button

        android:id="@+id/ok"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/entry"

        android:layout_alignParentRight="true"

        android:layout_marginLeft="10dip"

        android:text="OK"/>

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toLeftOf="@id/ok"

        android:layout_alignTop="@id/ok"

        android:text="Cancel"/>

    RelativeLayout>

2、 启动其它Activity并返回结果。运行效果如下,请编写程序。

  



package com.sise.Homework6_2;

 

import com.sise.Homework6_2.MainActivity;

import com.sise.Homework6_2.Activity02;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

 

public class MainActivity extends Activity{

       finalint Activity02=0x110;

       EditTextshow;

      

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       show = (EditText) findViewById(R.id.show);

              Buttonbtn = (Button) findViewById(R.id.bn1);

              btn.setOnClickListener(newOnClickListener(){

                     publicvoid onClick(View v){

                            Intentintent=new Intent(MainActivity.this,Activity02.class);

               startActivityForResult(intent,0x110);//密码暗号

                     }

              });

    }

   @Override

       protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {

              if(requestCode==0x110&&resultCode==0x112){

                     //请补充代码

           Bundle b=data.getExtras();

           String s=b.getString("txt");

           show.setText("另一个活动返回的数据:"+s);

              }

       }

   

}

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

    <TextView

        android:id="@+id/tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="用于显示返回结果"/>

    <EditText

        android:id="@+id/show"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>"

    <Button

        android:id="@+id/bn1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="切换到第二个Activity"

        android:layout_gravity="right"/>

LinearLayout>

package com.sise.Homework6_2;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

 

public class Activity02 extendsActivity  {

       EditTextev;

       @Override

       publicvoid onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.second);

              ev= (EditText) findViewById(R.id.ev);

              Buttonbn2 = (Button) findViewById(R.id.bn2);

              bn2.setOnClickListener(newOnClickListener() {

                     publicvoid onClick(View v) {

           //请补充代码

                     Intentintent=new Intent(Activity02.this,MainActivity.class);

                     Bundleb=new Bundle();

                     b.putString("txt",ev.getText().toString());

                     intent.putExtras(b);

                     Activity02.this.setResult(0x112,intent);

                     finish();

                     }

              });

       }

}

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

    <EditText

        android:id="@+id/ev"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:hint="请输入返回结果"/>

    <Button

        android:id="@+id/bn2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="返回结果到第一个Activity"

        android:layout_gravity="right"/>

LinearLayout>

3、有程序运行效果如下,请编写程序。

单击按钮“发送广播”,发送广播,广播接受之后在页面弹出Toast消息,如下图所示。单击按钮“启动服务”会启动名为MyService的服务。

 

  

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

   

   <EditText

       android:id="@+id/edit"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"/>

 

   <Button

       android:id="@+id/bn1"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:text="发送广播"/>

 

   <Button

       android:id="@+id/bn2"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:text="启动一个服务"/>

      

LinearLayout>

package com.sise.Key03;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

 

/*

 *创建人:小琦

 *创建时间:2017/5/3

 */

 

public class BroadcastMain extends Activity {

    @Override

    public voidonCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

        EditTextedit=(EditText)findViewById(R.id.edit);

        //获取程序界面中的按钮

        Buttonbn1=(Button)findViewById(R.id.bn1);

       bn1.setOnClickListener(new OnClickListener(){

            @Override

            public void onClick(View v){

                   //创建Intent对象

                   Intent intent=new Intent();

                   //设置Intent的Action属性

                   intent.setAction("com.sise.action.myBROADCAST");

                   intent.putExtra("msg","简单的消息");

                   //发送广播

                   sendBroadcast(intent);

            }

        });

    }

}

package com.sise.Key03;

 

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

 

public class MyReceiver extends BroadcastReceiver{

      public voidonReceive(Context context,Intent intent){

             Toast.makeText(context,"简单广播:接收到的Intent的Action为:"

                           +intent.getAction()

                           +"\n消息内容是:"+intent.getStringExtra("msg")

                           ,5000).show();    

      }

}

package com.sise.Key03;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class MyService extends Activity{

      public voidonCreate(Bundle savedInstanceState){

             super.onCreate(savedInstanceState);

             setContentView(R.layout.main);

             Buttonbn2=(Button)findViewById(R.id.bn2);

             //创建启动Service的Intent

             finalIntent intent=new Intent();

             //为Intent设置Action属性

             intent.setAction("com.sise.service.MyService");

             bn2.setOnClickListener(newOnClickListener(){

                    publicvoid onClick(View arg0){

                           //启动指定Service

                           startService(intent);

                    }

             });

      }

}

4、如下图,是一个注册页面,点击“注册”它就会跳转到注册成功的页面,并显示注册信息。在代码中给“注册”按钮加入相应监听,根据所学的知识补全下面的代码。

 

package com.sise.Key04;

 

import com.sise.model.Person;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

 

public class StartActivity extends Activity{

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       

       Button bn = (Button) findViewById(R.id.bn);

              bn.setOnClickListener(newOnClickListener() {

                     @Override

                     publicvoid onClick(View v) {

                            EditTextname = (EditText) findViewById(R.id.name);

                            EditTextpasswd = (EditText) findViewById(R.id.passwd);

                            RadioButtonmale = (RadioButton) findViewById(R.id.male);

                            Stringgender = male.isChecked() ? "男 " :"女";

                   //补全代码,完成数据绑定

                            //传递数据的单位Bundle

                Bundle data=new Bundle();

                Person person=newPerson(name.getText().toString(),passwd.getText().toString(),gender);

               data.putSerializable("person", person);

                Intent intent=new Intent(StartActivity.this,ResultActivity.class);

                intent.putExtras(data);

                startActivity(intent);

                     }

              });

    }

}

<?xmlversion="1.0"encoding="utf-8"?>

<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="请输入您的注册信息"

        android:textSize="20sp"/>

 

    <TableRow>

 

        <TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="用户名"

            android:textSize="16sp"/>

       

 

        <EditText

            android:id="@+id/name"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:hint="请填写想注册的帐号"

            android:selectAllOnFocus="true"/>

    TableRow>

 

    <TableRow>

 

        <TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="密码"

            android:textSize="16sp"/>

       

 

        <EditText

            android:id="@+id/passwd"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:password="true"

            android:selectAllOnFocus="true"/>

    TableRow>

 

    <TableRow>

 

        <TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="性别"

            android:textSize="16sp"/>

       

 

        <RadioGroup

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

 

            <RadioButton

                android:id="@+id/male"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text=""

                android:textSize="16sp"/>

 

            <RadioButton

                android:id="@+id/female"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text=""

                android:textSize="16sp"/>

        RadioGroup>

    TableRow>

 

    <TableRow>

    <Button

        android:id="@+id/bn1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="取消"/>

 

    <Button

        android:id="@+id/bn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="注册"

        android:textSize="16sp"/>

   TableRow>

TableLayout>

package com.sise.Key04;

 

import com.sise.model.Person;

 

import android.os.Bundle;

import android.widget.TextView;

import android.app.Activity;

import android.content.Intent;

 

public class ResultActivity extendsActivity{

       @Override

       publicvoid onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.result);

              TextViewname = (TextView) findViewById(R.id.name);

              TextViewpasswd = (TextView) findViewById(R.id.passwd);

              TextViewgender = (TextView) findViewById(R.id.gender);

              //补全代码,完成数据读取

              Intentintent=getIntent();   

              Bundledata=intent.getExtras();

              Personperson=(Person) data.get("person");

              name.setText("您的用户名:"+person.getName());

              passwd.setText("您的密码:"+person.getPassword());

              gender.setText("您的性别:"+person.getGender());

       }

}

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

   

    <TextView

        android:id="@+id/name"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="18sp"/>

    <TextView

        android:id="@+id/passwd"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="18sp"/>

    <TextView

        android:id="@+id/gender"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="18sp"/>

LinearLayout>

更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. 一句话锁定MySQL数据占用元凶
  3. Android测试-Monkey Test
  4. android review--基础知识
  5. Android小程序实现音乐播放列表
  6. Android:SwipeRefreshLayout和ViewPager滑动冲突的原因和正确的解
  7. Android(安卓)遍历Hashmap里面的key 和value
  8. 10、Android数据存储
  9. Android实践 -- 监听外置sdcard(TF卡)的插拔事件

随机推荐

  1. Android(安卓)TextSwitcher通知公告自动
  2. android通讯录根据手机号码查询姓名
  3. android 一键锁屏
  4. android xml 解析 修改
  5. android使用自定义属性
  6. Android五大布局(二)——RelativeLayout、T
  7. 推荐一系列优秀的Android开发源码
  8. ExpandableListView用法实例
  9. Android日常知识收集与总结之Android的Di
  10. Android UnitTest