Android初学之路(一)
2015-07-16 18:48
441 查看
之前要做单片机小项目——无线点阵广告牌!涉及到编写一个简单安卓软件......所以只有一点儿基础的我开始学习安卓了!第一天,当然学习一些简单了!主要是几个控件和监听的写法,还有Activity的生命周期等等!还有对XML的一些理解!本篇日志纯属个人足迹记录!不多说!今天写的代码贴上了!
首先是我写的第一个Activity,代码如下:
package com.example.helloandroid;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView.OnCloseListener;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button my_button01 = (Button) findViewById(R.id.Button01);
Button my_button02 = (Button) findViewById(R.id.Button02);
Button my_button03 = (Button) findViewById(R.id.Button03);
Button my_button04 = (Button) findViewById(R.id.Button04);
TextView my_textview01 = (TextView) findViewById(R.id.TestView01);
my_button01.setText(R.string.f_button);
my_button02.setText(R.string.msg);
my_button03.setText(R.string.cheng);
my_button04.setText(R.string.dig);
my_textview01.setText("hello");
my_button01.setOnClickListener(new Button_linstener());
/*
* 隐式实现监听,写法抽象,但是写代码快。
*/
my_button02.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("smsto://0800000123");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body", "你好");
startActivity(intent);
}
});
my_button03.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, OthersActivty02.class);
startActivity(intent);
}
});
my_button04.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, OthersActivty04.class);
startActivity(intent);
}
});
System.out.println("main on create");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("main on start");
}
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("main on restart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("main on resume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("main on pause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("main on stop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("main on destory");
}
/*
* 内部类实现监听
*/
class Button_linstener implements View.OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("testintent", "123");
intent.setClass(MainActivity.this, OthersActivity01.class);
MainActivity.this.startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0,1,1,R.string.exit);
menu.add(0,2,2,R.string.about);
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == 1){
finish();
}
return super.onOptionsItemSelected(item);
}
}
XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TestView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Button04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
之后的三个Activity
package com.example.helloandroid;
import com.example.helloandroid.MainActivity.Button_linstener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OthersActivty02 extends Activity{
private EditText edit01 = null;
private EditText edit02 = null;
private TextView text01 = null;
private Button button01 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other02);
edit01 = (EditText) findViewById(R.id.edit01);
edit02 = (EditText) findViewById(R.id.edit02);
text01 = (TextView) findViewById(R.id.text02);
button01 = (Button) findViewById(R.id.button02);
text01.setText(R.string.alu);
button01.setText(R.string.result);
button01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String f_edit = edit01.getText().toString();
String s_edit = edit02.getText().toString();
Intent intent = new Intent();
intent.putExtra("one", f_edit);
intent.putExtra("two",s_edit);
intent.setClass(OthersActivty02.this,OthersActivity03.class);
startActivity(intent);
}
});
}
}
首先是我写的第一个Activity,代码如下:
package com.example.helloandroid;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SearchView.OnCloseListener;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button my_button01 = (Button) findViewById(R.id.Button01);
Button my_button02 = (Button) findViewById(R.id.Button02);
Button my_button03 = (Button) findViewById(R.id.Button03);
Button my_button04 = (Button) findViewById(R.id.Button04);
TextView my_textview01 = (TextView) findViewById(R.id.TestView01);
my_button01.setText(R.string.f_button);
my_button02.setText(R.string.msg);
my_button03.setText(R.string.cheng);
my_button04.setText(R.string.dig);
my_textview01.setText("hello");
my_button01.setOnClickListener(new Button_linstener());
/*
* 隐式实现监听,写法抽象,但是写代码快。
*/
my_button02.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("smsto://0800000123");
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body", "你好");
startActivity(intent);
}
});
my_button03.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, OthersActivty02.class);
startActivity(intent);
}
});
my_button04.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, OthersActivty04.class);
startActivity(intent);
}
});
System.out.println("main on create");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("main on start");
}
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("main on restart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("main on resume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("main on pause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("main on stop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("main on destory");
}
/*
* 内部类实现监听
*/
class Button_linstener implements View.OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("testintent", "123");
intent.setClass(MainActivity.this, OthersActivity01.class);
MainActivity.this.startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0,1,1,R.string.exit);
menu.add(0,2,2,R.string.about);
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == 1){
finish();
}
return super.onOptionsItemSelected(item);
}
}
XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TestView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Button04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
之后的三个Activity
package com.example.helloandroid;
import com.example.helloandroid.MainActivity.Button_linstener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OthersActivty02 extends Activity{
private EditText edit01 = null;
private EditText edit02 = null;
private TextView text01 = null;
private Button button01 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other02);
edit01 = (EditText) findViewById(R.id.edit01);
edit02 = (EditText) findViewById(R.id.edit02);
text01 = (TextView) findViewById(R.id.text02);
button01 = (Button) findViewById(R.id.button02);
text01.setText(R.string.alu);
button01.setText(R.string.result);
button01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String f_edit = edit01.getText().toString();
String s_edit = edit02.getText().toString();
Intent intent = new Intent();
intent.putExtra("one", f_edit);
intent.putExtra("two",s_edit);
intent.setClass(OthersActivty02.this,OthersActivity03.class);
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/edit01" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/text02" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/edit02" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/button02" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
package com.example.helloandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class OthersActivity01 extends Activity{ private TextView my_textview02 = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other01); Intent intent = getIntent(); String value = intent.getStringExtra("testintent"); my_textview02 = (TextView) findViewById(R.id.TextView02); my_textview02.setText(value); System.out.println("01 on create"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("01 on start"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("01 on restart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("01 on resume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("01 on pause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("01 on stop"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("01 on destory"); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_name"/> </LinearLayout>
package com.example.helloandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class OthersActivity03 extends Activity{ private TextView mytext = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other03); mytext = (TextView) findViewById(R.id.result_test); Intent intent = getIntent(); String f_str = intent.getStringExtra("one"); String s_str = intent.getStringExtra("two"); int f_int = Integer.parseInt(f_str); int s_int = Integer.parseInt(s_str); int results = f_int * s_int; mytext.setText(results + ""); } }
package com.example.helloandroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class OthersActivty04 extends Activity{ private Button back_button = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other04); back_button = (Button) findViewById(R.id.dig_button); back_button.setText(R.string.dig_back); back_button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); System.out.println("04 on create"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("04 on start"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("04 on restart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("04 on resume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("04 on pause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("04 on stop"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("04 on destory"); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/result_test" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/dig_text" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/dig_button" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HelloAndroid</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="exit">退出</string> <string name="about">关于</string> <string name="alu">乘以</string> <string name="result">等于</string> <string name="cheng">乘法器</string> <string name="f_button">我的第一个按钮</string> <string name="msg">短信</string> <string name="dig_back">返回第一个Activity</string> <string name="dig">小窗口Activity</string> </resources>
相关文章推荐
- android studio使用
- iMX6QD How to Add 24-bit LVDS Support in Android
- 导入Android项目时遇到的问题
- Android 判断EditView是否为密码框
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android中xml文件的使用
- android:showAsAction用法
- android RecyclerView 资源汇总
- Android Shader 颜色、图像渲染 paint.setXfermode
- 基于Android的计步器(Pedometer)的讲解(一)——Fragment页面跳转
- 【实习项目记录】(四)Android 实现手机验证时,按钮倒计时60s
- android 开源库
- android arm
- 黑马52期最新android视频完整高清不加密
- 利用半透明对话框实现新浪微博android客户端首次运行时的提示界面
- Android仿真翻页
- android安装包无法覆盖
- AndroidStudio错误local path doesn't exist解决办法
- Android 触摸屏幕产生小气泡的效果源码
- Android TextView 文字飞入效果