博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

20155217 实验四 Android程序设计

Posted on 2017-05-20 15:50  20155217杨笛  阅读(124)  评论(0编辑  收藏  举报

20155217 实验四 Android程序设计

任务一:

完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号。
  • R.java文件是定义该项目所有资源的索引文件。一般R类是下面这种格式:
public final class R {
    public static final class attr{
        ...
    }
    public static final class drawable{
        ...
    }
    public static final class layout{
        ...
    }
    public static final class string{
        ...
    }
}
  • 这个文件将使程序中资源的使用变得更加方便。由于这个文件不能被手动编译,所以当我们在项目中加入新的资源时,只需要刷新一下该项目即可。

  • 在后面编写程序时出现了“无法找到R类”这样的错误,根据狄惟佳同学的博客中 Android Studio中R文件丢失的解决办法来寻找解决方法。

  • 对于这个任务,我们只需要将布局文件activity_main.xml中相应的内容稍做修改:
    将其中的android:text="Hello World!"改为android:text="Hello World!\n\t20155217"即可。

  • 运行结果如下:

任务二:

创建 ThirdActivity, 在ThirdActivity中显示自己的学号,修改代码让MainActivity启动ThirdActivity
  • 其中在这个任务中,要求让MainActivity启动ThirdActivity,所以还需要修改MainActivity,相应代码段改为:
@Override
    public boolean onTouch(View arg0, MotionEvent event) {
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("message", "20155217杨笛");
        startActivity(intent);
        return true;
    }
  • 运行结果如下:

任务三:

修改代码让Toast消息中显示自己的学号信息。
  • activity_main示例如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dragon.toast.Main">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="220155217杨笛"
        android:id="@+id/btn1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="31dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
  • MainActivity.java示例如下:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnshow1 = (Button) findViewById(R.id.btn1);
        btnshow1.setOnClickListener(new View.OnClickListener()
                                    {
                                        @Override
                                        public void onClick(View v){
                                            Toast toast = Toast.makeText(MainActivity.this,"20155217杨笛", Toast.LENGTH_LONG);
                                            toast.show();
                                        }
                                    });
  • 运行结果如下:

任务四:

修改布局让P290页的界面与教材不同。
  • 修改结果为:
<TextView   
    android:id="@+id/tv1"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:textSize="50sp"  
    android:textColor="#000000"  
    android:text="20155217"/>  
<TextView   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:textSize="40sp"  
    android:textColor="#ffff00"    
    android:text="杨笛"/> 
  • 运行结果:

任务五:

运行教材本章相关代码并截图
  • 以课本P298为例,代码如下:
int counter = 0;
    int[] colors = { Color.BLACK, Color.BLUE, Color.CYAN,
            Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY,
            Color.MAGENTA, Color.RED, Color.WHITE, Color.YELLOW };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it
// is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    public void changeColor(View view) {
        if (counter == colors.length) {
            counter = 0;
        }
        view.setBackgroundColor(colors[counter++]);
    }
  • 当用户按下(或触碰)时钟的时候,会调用该方法并接受时钟对象。要修改时钟的颜色,需要调用其setBackgroundColor方法,传入一个颜色对象,从而实现触碰时钟改变颜色。
  • 运行结果如下: