zoukankan      html  css  js  c++  java
  • Building Your First App

    http://www.cnblogs.com/gcg0036/p/4321246.html

    Building a Simple User Interface:

    image

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" > 
    
        <EditText
            android:id="@+id/edit_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/edit_message" /> 
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send" /> 
    
    </LinearLayout>

    关于一些属性的说明:

    Android:id

    这里定义的是View的唯一标示符,你可以在程序的代码里进行引用
    +号只是当你第一次定义一个资源ID的时候需要。这里是告诉SDK此资源ID需要被创建出来。在应用程序被编译之后,SDK就可以直接使用ID值,edit_message是在项目gen/R.java文件中创建一个新的标示符,这个标示符就和EditText关联起来了。一旦资源ID被创建了,其他资源如果引用这个ID就不再需要+号了,。这里是唯一一个需要+号的属性

    Android:layout_width 和android:layout_height

    对于宽和高不建议指定具体的大小,使用"wrap_content"指定之后,这个视图只是占据内容大小的空间。如果你使用了"fill_parent",这时EditText将会布满整个屏幕,因为它将适应父布局的大小

    Android:hint

    当文本框为空的时候,会默认显示这个字符串。对于字符串"@string/edit_message"的值所引用的资源应该是定义在单独的文件里,而不是直接使用字符串。因为使用的是值是存在的资源,所以不需要使用+号。

    这个按钮不需要指定android:id的属性,因为在Activity代码里不被引用到。

    对于文本框来说如果能够沾满整个宽度会更好。LinearLayout使用权重的属性来达到这个目的,你可以使用android:layout_weight属性来设置。

    你可以根据每一个部件所占的空间来指定圈中值的大小,它的总数是有同级别的部件来决定的。就类似于饮料的成分配方:“两份伏特加酒,一份咖啡利口酒”,意思就是这个酒中伏特加酒占三分之二。例如,你设置一个View的权重是2,另一个View的权重是1,那么总数就是3,这时第一个View占据 2/3的空间,第二个占据1/3的空间。如果你再加入第三个View,权重设为1,那么第一个View会占据1/2的空间,剩余的被另外两个View平分。

    对于所有的View默认的权重是0,如果你只设置了一个View的权重大于0,那么这个View将占据除去别的View本身占据的空间的的所有剩余空间。因此这里设置EditText的权重为1,使其能够占据除了按钮之外的所有空间。

    为了达到更有效的布局,在你设置权重的时候,你应该把EditText的宽度设置为0

     

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">DemoAndroid</string>
        <string name="hello_world">Hello world!</string>
        <string name="action_settings">Settings</string>
        <string name="edit_message">Enter a message</string>
        <string name="button_send">Send</string>
    
    </resources>

    注意: 该字符串资源与id使用了相同的名称(edit_message)。然而,对于资源的引用是区分类型的(比如id和字符串),因此,使用相同的名称不会引起冲突。

    image

    Starting Another Activity:

    Respond to the Send Button-响应Send(发送)按钮

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
    /** 当用户点击Send按钮时调用 */
      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);
      }

    请注意,为了让系统能够将这个方法(你刚在MyFirstActivity中添加的sendMessage方法)与在android:onClick属性中提供的方法名字匹配,它们的名字必须一致,特别是,这个方法必须满足以下条件:

    • 公共的
    • 没有返回值
    • 有一个唯一的视图(View)参数(这个视图就是将被点击的视图)

    在这个Intent构造函数中有两个参数:第一个参数是Context(上下文)(之所有可以用this是因为当前Activity(MyFirstActivity)是Context的子类) 系统需要传递Intent的应用组件的class对象(在这个案例中,这个activity应该被启动)

    Intent 可以携带各种数据类型的集合来作为key-value附加对。 putExtra() 方法把键名作为第一个参数,把值作为第二个参数。

    通常使用应用程序包名作为前缀来定义意图键是很好的做法。如果应用程序与其他应用程序进行交互就可以确保意图键唯一。

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    启动一个Activity,你只需要调用startActivity()方法然后传入你的Intent(意图)系统接收到你的请求后会实例化在Intent中指定的Activity

    image

    片段把应用程序的功能和用户界面分解成可以重新使用的模块

    活动所有子类都必须实现 onCreate()方法。创建活动新实例时系统会调用该方式,此时必须用 setContentView()来定义活动布局,而且应对活动组件进行初始设置。

    <activity
                android:name=".DisplayMessageActivity"
                android:label="@string/title_activity_display_message"
                android:parentActivityName=".MainActivity" >
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value="com.amIurs.MainActivity" />
            </activity>

    android:parentActivityName属性在应用程序逻辑层次结构中声明了活动的父类活动名称。

    系统使用此值来实现默认导航操作,比如navigationon安卓4.1(API级别16)或者更高版本。

    使用支持库并且如下所示添加 <meta-data>元素可以为安卓旧版本提供相同功能

    Receive the Intent-获取Intent(意图)

    每一个被Intent调用的Activity,不管用户将它导航到哪,你都可以在启动的Activity中通过getIntent()方法得到Intent以及Intent包含的数据

    public class DisplayMessageActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // 从intent中获取信息
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    
        // 创建TextView对象
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
    
        setContentView(textView);
        }

    image image

    sdf

  • 相关阅读:
    .net core读取appsettings.config中文乱码问题
    vs2017错误:当前页面的脚本发生错误
    VS Code中无法识别npm命令
    Visual Studio报错/plugin.vs.js,行:1074,错误:缺少标识符、字符串或数字
    记录一次在生成数据库服务器上出现The timeout period elapsed prior to completion of the operation or the server is not responding.和Exception has been thrown by the target of an invocation的解决办法
    Java集合框架
    java hash表
    Java Dictionary 类存储键值
    java数据结构 栈stack
    java封装
  • 原文地址:https://www.cnblogs.com/gcg0036/p/4321246.html
Copyright © 2011-2022 走看看