一个Android程序 Helloworld
2011-03-24 21:37
525 查看
HelloActivity.java
package mars.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HelloActivity extends Activity {
private Button myButton1 = null;
private Button myButton2 = null;
private TextView myEditText = null;
private TextView myTextView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//设置Activity的布局
myButton1 = (Button)findViewById(R.id.myButton1);//得到按钮对象s
myButton2 = (Button)findViewById(R.id.myButton2);
myEditText = (TextView)findViewById(R.id.myEditText);//得到TextView对象
myTextView = (TextView)findViewById(R.id.myTextView);
myButton1.setOnClickListener(new View.OnClickListener() {//给按钮注册监听器
public void onClick(View v) {
myTextView.setText(myEditText.getText());
}
});
myButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myTextView.setText(new String());
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.helloworld"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myButtonText1"
/>
<Button
android:id="@+id/myButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myButtonText2"
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloActivity!</string>
<string name="app_name">helloworld</string>
<string name="myButtonText1">Click Button One</string>
<string name="myButtonText2">Click Button Two</string>
</resources>
package mars.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HelloActivity extends Activity {
private Button myButton1 = null;
private Button myButton2 = null;
private TextView myEditText = null;
private TextView myTextView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//设置Activity的布局
myButton1 = (Button)findViewById(R.id.myButton1);//得到按钮对象s
myButton2 = (Button)findViewById(R.id.myButton2);
myEditText = (TextView)findViewById(R.id.myEditText);//得到TextView对象
myTextView = (TextView)findViewById(R.id.myTextView);
myButton1.setOnClickListener(new View.OnClickListener() {//给按钮注册监听器
public void onClick(View v) {
myTextView.setText(myEditText.getText());
}
});
myButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myTextView.setText(new String());
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.helloworld"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myButtonText1"
/>
<Button
android:id="@+id/myButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myButtonText2"
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloActivity!</string>
<string name="app_name">helloworld</string>
<string name="myButtonText1">Click Button One</string>
<string name="myButtonText2">Click Button Two</string>
</resources>
相关文章推荐
- Android HelloWorld程序
- 一个闪屏的Android示例程序-z
- android多activity退出整个程序的一个实现
- 编写一个程序,当程序输入Ctrl+C时,输出字符串“ Ctrl+C I got signal”,输入第5次时,程序退出 ,在其余时间,改程序循环打印helloworld信息
- 【RichieZhu】Android多activity退出整个程序的一个实现
- 一个简单、漂亮、功能强大的Android日志程序:logger
- Android 中在一个程序中启动另一程序
- 编写一个Android Linux内核驱动并用C可执行程序测试
- android的第一个程序helloworld (有图有真相)
- Java Learning 001 新建一个Java工程 HelloWorld程序
- Android Studio新建一个HelloWorld 程序(App)
- Android开发视频教程之三(helloworld程序的编写与分析)
- 关于使用eclipse创建Android程序时会多出一个appcompat_v7
- .Net 转战 Android 4.4 日常笔记(2)--HelloWorld入门程序
- 解决eclipse每次Run as Android 程序,会重新启动一个AVD模拟器的问题
- 开发的一个android程序,总是显示无法运行,就退出了。但是没有报错。求大神解救啊!
- Android一个程序使用另一个程序的资源
- Android 在一个程序中启动另一个程序
- 如何在android程序执行初始化的时候弹出一个PopupWindow 触发的事件是在初始化的时候。
- android 在一个Activity中结束另一个Activity的方法(可以完全退出程序)