【编程】我的第二个android程序-按钮+文字

我的第二个android程序实现的是点击不同的按钮显示不同的文字,实现的最终界面如下:

 

一、布局

1.布局主要分两个区域,文字区和按钮区;

2.布局区部分源代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">

    <EditText 
        android:inputType="number"
        android:id="@+id/editor"
        android:layout_width="fill_parent" android:layout_height="0dip"
        android:autoText="true"
        android:capitalize="sentences"
        android:layout_weight="1"
        android:cursorVisible="false"
        android:paddingLeft="120dip"
        android:freezesText="true" >
        <requestFocus />
    </EditText>

    <!-- Next view is another linear layout manager, now horizontal.  We
         give it a custom background; see colors.xml for the definition
         of drawable/semi_black-->

    <LinearLayout
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center" android:gravity="right"
        android:orientation="horizontal"
        android:background="@drawable/semi_black">

        <Button android:id="@+id/sun" style="@style/ActionButton"
            android:text="@string/sun" />

        <Button android:id="@+id/moon" style="@style/ActionButton"
            android:text="@string/moon" android:textColor="@color/red" />
        
        <Button android:id="@+id/star" style="@style/ActionButton"
            android:text="@string/star" />
    </LinearLayout>

</LinearLayout>

二、java程序

package com.example.android.skeletonapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * This class provides a basic demonstration of how to write an Android
 * activity. Inside of its window, it places a single view: an EditText that
 * displays and edits some internal text.
 */
public class SkeletonActivity extends Activity {
    
    private EditText mEditor;
    
    public SkeletonActivity() {
    }

    /** Called with the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate our UI from its XML layout description.
        setContentView(R.layout.skeleton_activity);//显示定义的布局

        // Find the text editor view inside the layout, because we
        // want to do various programmatic things with it.
        mEditor = (EditText) findViewById(R.id.editor);

        // Hook up button presses to the appropriate event handler.
        ((Button) findViewById(R.id.sun)).setOnClickListener(mSunListener);
        ((Button) findViewById(R.id.moon)).setOnClickListener(mMoonListener);
        ((Button) findViewById(R.id.star)).setOnClickListener(mStarListener);
        
    }

    /**
     * Called when the activity is about to start interacting with the user.
    */ 
    @Override
    protected void onResume() {
        super.onResume();
    }
    
       /**
     * A call-back for when the user presses the sun button.
     */
    OnClickListener mSunListener = new OnClickListener() {
        public void onClick(View v) {
        	mEditor.setText(getText(R.string.main_label_1));//当点击sun健时,显示sun!
        }
    };

    /**
     * A call-back for when the user presses the moon button.
     */
    OnClickListener mMoonListener = new OnClickListener() {
        public void onClick(View v) {
        	mEditor.setText(getText(R.string.main_label_2));//当点击moon健时,显示moon!
        }
    };
    
    /**
     * A call-back for when the user presses the star button.
     */
    OnClickListener mStarListener = new OnClickListener() {
        public void onClick(View v) {
        	mEditor.setText(getText(R.string.main_label_3));//当点击star健时,显示star!
        }
    };
}

三、积累(我把自己写程序过程中,记录的问题)

1.class前面的public的作用是什么。

        public属于“访问权限”问题。

类创建一个对象后,该对象可以通过“.”运算符访问自己的变量,并使用类中的方法。但访问这些变量和使用类中的方法有一定的限制,通过修饰符private、protected、public来说明访问权限。

       “private”——修饰私有变量和私有方法,意味着只有在本类中创建该类对象时,该对象才能访问自己的私有成员变量;

       “public”——修饰共有变量和共有方法,他们可以被后续定义的任何类访问和引用。

2.关于布局layout

        R.layout.hello_activity指的是res/layout/hello_activity;

该目录下有文件(hello_activity.xml)的具体内容如下:

       <EditText

       xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/text" // 为当前控件分配一个ID

       android:layout_width="fill_parent" //宽度布满整个屏幕

       android:layout_height="fill_parent" //高度布满整个屏幕

       android:textSize="18sp"  //字体大小,使字体最大化;

       android:autoText="true"  //如果自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。

       android:capitalize="sentences" //设置英文字母大写类型。第一个字母大写;android:text="@string/hello_activity_text_text"/>  //指向字符串

       注:XML是Android程序的资源,即它不属于源代码,但会被编译进应用程序。

3.如何看懂XML文件

       查清自己需要理解的XML;

4.android程序的入口地址为(目前认为)我们所创建的Activity.

5.关于android:id="@+id/editor"的进一步深入学习

       android中的组件需要用一个int类型的值来表示,这个值就是组件标签中的id属性值。

id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc,@+id/xyz等。

如果在@后面使用“+”,表示当修改完某个布局文件并保存后,系统自动在R.java文件中生成相应的int类型变量。

       例如,@+id/xyz会在R.java文件中生成int xyz = value。

       注意:既然组件的id属性是一个资源id就可以,那么自然可以设置任何已经存在的资源id值,例如,@drawable/icon、@string/ok、@+string/you。

6.布局中的坐标如何确定

       从上到下,满足每一个的要求。

       padding是填充的意思;

7.italic斜体

8.onResume() 是在Activity第一次创建后,重新加载实例时调用。例如,(1)打开APP第一个界面OnCreate后,调用onResume然后切换到下一个界面,那么第一个界面不finish按Back键回来时,就调onResume不调onCreate;(2)APP用到一般时,Home键切出去,在回来时调OnResume;

9.图片定位不准确既使编译成功,会导致安装失败;

10.目前的屏为480*320

11.LinearLayout

       LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值