设为首页 加入收藏

TOP

Android开发初体验(五)
2017-10-12 11:15:06 】 浏览:10082
Tags:Android 开发 体验
android:text="@string/false_button"/> </LinearLayout> <Button android:id="@+id/cheat_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:text="@string/cheat_button"/> <Button android:id="@+id/pre_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|left" android:text="@string/pre_button" android:drawableLeft="@drawable/arrow_left" android:drawablePadding="4dp"/> <ImageButton android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:src="@drawable/arrow_right" android:contentDescription="@string/next_button"/> </FrameLayout>
  • 保存数据以应对设备旋转

覆盖以下Activity方法

protected void onSaveInstanceState(Bundle outState)
  • 该方法通常在onStop()方法之前由系统调用,除非用户按后退键。
  • 该方法的默认实现要求所有activity视图将自身数据状态保存在Bundle对象中。Bundle是存储字符串键与限定类型值之间映射关系(键值对)的一种结构。
public class QuizActivity extends AppCompatActivity {
    ......
    private int mCurrentIndex=0;
    private static final String KEY_INDEX="index";
    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
      
        savedInstanceState.putInt(KEY_INDEX,mCurrentIndex);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        
        if(savedInstanceState!=null){
            mCurrentIndex=savedInstanceState.getInt(KEY_INDEX,0);
        }
        ......
    }
    ......
}

7. 日志

public static int d(String tag,String msg)//输出日志信息d:debug

方法的第一个参数通常是以类名为值的TAG常量传入

public class QuizActivity extends AppCompatActivity {
    private static final String TAG="QuizActivity";
    ......
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.d(TAG,"onCreate(Bundle) called");
        
        setContentView(R.layout.activity_quiz);
      
        ......
    }
}
日志级别 方法 说明
ERROR Log.e(...) 错误
WARNING Log.w(...) 警告
INFO Log.i(...) 信息型消息
DEBUG Log.w(...) 调试输出
VERBOSE Log.v(...) 仅用于开发

所有的日志记录方法都有两种参数签名:string类型的tag参数和msg参数;除tag和msg参数外再加上Throwable实例参数

9. 第二个activity

新activity将带来第二个用户界面,方便用户偷看问题的答案

第二个activity的布局组件的定义(activity_cheat.xml)

<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="center"
    android:orientation="vertical"
    tools:context="com.example.mdx.studyworks.CheatActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/warning_text"/>
    <TextView
        an
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android开发初体验 下一篇Android开发初体验

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目