zoukankan      html  css  js  c++  java
  • 学习android文档

    follow lesson,

    一. 创建一helloworld,运行。fragment_main.xml里默认是relativeLayout和Textview

    二. 创建第一个图形界面,主要是说fragment_main.xml里放各种元素的位置,布局等,比如button,TextView等

    文章原文层次是:

    Building a Simple User Interface:

    1. Create a Linear Layout in fragment_main.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:orientation
    ="horizontal" >
    </LinearLayout>

    2. Add a Text Field inside the <LinearLayout>

    <EditText android:id="@+id/edit_message"         
    android:layout_width
    ="wrap_content"
    android:layout_height
    ="wrap_content"
    android:hint
    ="@string/edit_message" />

    3. Add String Resources  at res/values/strings.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <resources>     
    <string name="app_name">My First App</string>     
    <string name="edit_message">Enter a message</string>     
    <string name="button_send">Send</string> <string name="action_settings">Settings</string> <string name="title_activity_main">MainActivity</string> </resources>

    4. Add a Button to fragment_main.xml

     <Button         
    android:layout_width="wrap_content"
    android:layout_height
    ="wrap_content"
    android:text
    ="@string/button_send" />

    5. Make the Input Box Fill in the Screen Width,输入框看起来更美观

    <EditText
      android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="@string/edit_message" />

    图形化的用了LinearLayout,取代RelativeLayout

    可编辑的文本框是<EditText>

    <EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />

    res里设计显示的图片啦,文字啦等,这里在res/value/strings.xml中加入了fragment_mail里定义的button显示的文字,编辑框默认输入文字等.@表示全局的,+表示这个id可以被引用

    三、学习Activity

    效果是,当user点击前面添加的send按钮时,开启新的一页

    1. Respond to the Send Button:add onClick event in fragment_main.xml

    <Button     
    ...
    android:onClick
    ="sendMessage" />

    说明:sendMesage是在MainActivity.java中自定义的方法,能被onClick的方法必须满足

    1)public的

    2) 返回为void的

    3)view作为唯一的parameter

    因此,

    2. 创建sendMessage方法。在MainActivity.java中代码如下:

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) { // Do something in response to button }

    3. 创建 an Intent inside sendMessage()。里面用到了Intent

    In Eclipse, press Ctrl + Shift + O to import missing classes

    插曲:this()的含义,表示显式调用本类构造方法,需要放第一行;super()是调用父类构造方法;那么如果class B extends A,B(){this(10)}时,this调用的先是父类的构造方法,再是子类的。很方便啊。

    extras是指,一个Intent携带了一个数据类型迥异的集合,作为key-values对。putExtra方法第一个参数对应key,第二个对应value.key需要是个常量,为的是让后面的Activity可以通过这个常量查询到这个value。

    插曲:

    在linux安装时间,很多次用到

    1. ./configure --prefix

    这个命令,但不了解命令到底是配置那些选项?

    最佳答案:

    源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)

    Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./configure –help输出详细的选项列表。

    其中--prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较凌乱。

    如果配置--prefix,如:

    1. ./configure --prefix=/usr/local/test

    可以把所有资源文件放在/usr/local/test的路径中,不会杂乱。

    用了—prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。

    当然要卸载程序,也可以在原来的make目录下用一次make uninstall,但前提是make文件指定过uninstall。

     4. Start the Second Activity by startActivity(intent);

    step3和step4的code:

    /** Called when the user clicks the Send button */ 
    public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
    }

    5. Create the Second Activity :DisplayMessageActivity,Add the title string,add it to AndroidMenifest.xm文件中。

    所有继承Activity的子类必须实现onCreate()方法。这个方法中必须使用setContentView()方法定义Activity布局,以及组件初始化设置。

    所有的Activities必须要在AndroidMenifest文件中声明。使用Eclipse会自动生成的。

    Meta-data:元数据,最本质最抽象的数据。

    6. 以上步骤之后,apk可以运行,但是无法传递输入的文本。怎么办呢?获取上一个Activity中的信息,需要使用

    1》 借助Intent-- 每个Activity都离不了Intent,使用getIntent()方法获取某个你需要的Activity及检索数据。在Oncreate()方法中加上:

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    2》创建TextView来展示信息 -- 创建TextView并通过setText()来赋值给它;记得要把这个TextView添加到布局xml中,这次我们使用函数setContenView搞一搞:-)在Oncreate()方法中添加。完毕后整体Oncreate()为:

    @Override public void onCreate(Bundle savedInstanceState) {     
    super.onCreate(savedInstanceState); // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);
    // Set the text view as the activity layout
    setContentView(textView);
    }
  • 相关阅读:
    BZOJ 1093: [ZJOI2007]最大半连通子图
    BZOJ 1406: [AHOI2007]密码箱
    BZOJ 1073: [SCOI2007]kshort
    BZOJ 1857: [Scoi2010]传送带
    AC日记——天天爱跑步 洛谷 P1600
    AC日记——[Sdoi2010]粟粟的书架 bzoj 1926
    AC日记——The Shortest Path in Nya Graph hdu 4725
    AC日记——文化之旅 洛谷 P1078
    AC日记——【模板】分块/带修改莫队(数颜色) 洛谷 P1903
    AC日记——大爷的字符串题 洛谷 P3709
  • 原文地址:https://www.cnblogs.com/ianthe/p/3675793.html
Copyright © 2011-2022 走看看