完全没有接触过Java,更不用说利用Java语言在eclipse上尝试开发安卓应用程序了。不过既然和师兄打赌了,就要努力兑现自己的承诺。在七天以内开发出对得起自己的应用程序,显然不能太烂,我从昨天开始网上查资料学习,终于是学到了自己想要学的一点知识了,未来的路还很长,我要加油咯~
我昨天学习了“如何利用button控件来获得响应,进入另一个已经设定好的页面”,刚刚入门,还需慢慢成长。
在这里将学习到的东西做一个总结,即可以用来帮助需要的人,也作为我学习成长路程上的一种标记。
Android 利用button控件 进入自己设定的另一个界面
1、在res->values->strings.xml定义变量
e.g:
<?xml version="1.0"encoding="utf-8"?>
<resources> //资源文件:string name是定义的变量名=“神马”与</string>中间夹带的是这个变量的值
<string name="hello">Hello World,MyFirstAndoridAppActivity!</string>
<string name="app_name">MyFirstAndoridApp</string>
<string name="button">press this button</string>
<stringname="press_button">这是Hookc开发的第一个应用程序喔~谢谢支持!</string>
<color name=”background”>#244</color>
</resources>
资源文件类型有“color”设置颜色、”string”字符串类型等,具体可在strings.xml中add
背景色应该在main.xml中的
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>
中加入
android:background="@color/background" //引号里面为已定义的变量
2、在res->layout->mian.xml中排布主要页面
e.g:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<Button
android:id="@+id/trigger"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:text="@string/button"
/>
</LinearLayout>
设置控件的属性和引用已定义的变量,如TextView< />表示文本控件,Button< />表示按钮控件,android:layout_width和android:layout_heigth分别表示控件在屏幕上的位置属性。
关于属性单位的资料:
/*Historically, programmers always designed computer interfaces
in terms of pixels. For example, you mightmake a field 300 pixels
wide, allow 5 pixels of spacing between columns, and define
icons 16-by-16 pixels in size. The problem is that if you run that
program on new displays with more and more dots per inch
(dpi), the user interface appears smaller and smaller. At some
point, it becomes too hard to read.
Resolution-independent measurements help solve this problem.
Android supports all the following units:
• px (pixels): Dots on the screen.
• in (inches): Size as measured bya ruler.
• mm (millimeters): Size asmeasured by a ruler.
• pt (points): 1/72 of an inch.
• dp (density-independent pixels):An abstract unit based
on the density of the screen. On a display with 160 dots
per inch, 1dp = 1px.
• dip: Synonym for dp, used moreoften in Google examples.
• sp (scale-independent pixels):Similar to dp but also scaled
by the user’s font size preference.
To make your interface scalable to any current and future type
of display, I recommend you always use the sp unit for text sizes
and the dip unit for everything else. Youshould also consider
using vector graphics instead of bitmaps (see Chapter 4, Explor-
ing2D Graphics, onpage 73).*/
3、在layout文件夹里面点击右键加入一个新的Android XML File 用于编辑点击后显示的内容
e.g:
<?xmlversion="1.0"encoding="utf-8"?>
<ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/press_button"/> //在页面中显示文本内容
</LinearLayout>
</ScrollView>
4、点击File->New->Class新建一个java的类,用于和layout中的设计另一个页面的xml绑定起来
e.g:
package my.First.test;
import android.app.Activity; //文件包
import android.os.Bundle;
public classpress_this_button extends Activity{
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.press_this_button);
//这里的press_this_button是layout中的新建的那个xml的名字
}
}
5、在主要代码文件即MyFirstAppActivity.java中定义点击按钮的映射函数,点击按钮的动作函数也需要自己定义
e.g:
package my.First.test;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener; //监听用户动作的头文件
public classMyFirstAndoridAppActivityextendsActivity implements OnClickListener {
/** Called when the activity is firstcreated. */
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
ViewcontinueButton = findViewById(R.id.trigger); //trigger是按钮的id
continueButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.trigger:
Intenti = newIntent(this,press_this_button.class);
//有这个点击按钮的动作时,就进入press_this_button这个类中
startActivity(i);
break;
// More buttons go here (if any) ...
}
}
}
6、最重要的一点,任何活动(Activity)(包括按钮…..)都必须在一开始就有的AndroidMainifest.xml中声明,否则无法进行点击
e.g:
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="my.First.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdkandroid:minSdkVersion="15"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MyFirstAndoridAppActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
!!!<activityandroid:name=".press_this_button"
android:label="@string/button">
</activity> //新增的活动要在前一个活动结束后进行
来自“.press_this_button”这个类的string类型的button对象
</application>
</manifest>