第一个android开发应用hello world!
activity_main.xml文件代码:
<RelativeLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:id="@+id/show"
/>
<!-- 设置按钮的文本为“显示” -->
<Button android:text="显示"
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- 设置按钮的文本为“隐藏” -->
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="隐藏" />
</RelativeLayout>
MainActivity.java文件代码:
package com.example.hello;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置使用main.xml文件定义的界面布局
setContentView(R.layout.activity_main);
//获取UI界面中ID为R.id.ok的按钮
Button bn = (Button)findViewById(R.id.ok);
//为按钮绑定一个单机事件的监听器
bn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//获取UI界面中ID为R.id.show的文本框
final TextView show = (TextView)findViewById(R.id.show);
//改变文本框的文本内容
show.setText("hello android!!!"+new java.util.Date());
}
});
//获取UI界面中ID为R.id.cancel的按钮
Button bn1 = (Button)findViewById(R.id.cancel);
//为按钮绑定一个单机事件的监听器
bn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//获取UI界面中ID为R.id.show的文本框
final TextView show = (TextView)findViewById(R.id.show);
//改变文本框的文本内容
show.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}