Android初学之路(一)
来源:互联网 发布:iphone实用软件排行 编辑:程序博客网 时间:2024/05/17 03:45
之前要做单片机小项目——无线点阵广告牌!涉及到编写一个简单安卓软件......所以只有一点儿基础的我开始学习安卓了!第一天,当然学习一些简单了!主要是几个控件和监听的写法,还有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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubUri 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(MainActivity.this, OthersActivty02.class);startActivity(intent);}}); my_button04.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent 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 stubsuper.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{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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");}@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();System.out.println("01 on start");}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.onRestart();System.out.println("01 on restart");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();System.out.println("01 on resume");}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();System.out.println("01 on pause");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();System.out.println("01 on stop");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfinish();}});System.out.println("04 on create");}@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();System.out.println("04 on start");}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.onRestart();System.out.println("04 on restart");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();System.out.println("04 on resume");}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();System.out.println("04 on pause");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();System.out.println("04 on stop");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.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>
0 0
- Android初学之路(一)
- Android初学之Service(一)
- Android初学之Activity(一)
- js 初学之路(一)
- PHP 初学之路【一】
- GitHub 之初学(一)
- android 初学教程(一)
- Android Studio初学(一)
- Android初学之路(二)
- PHP学习之路(一)——初学PHP
- python+django初学之安装(一)
- 初学C++之通用工具(一)
- Maven 之初学笔记(一)
- 初学 nginx 之location(一)
- Java初学之集合(一)
- Java初学之IO(一)
- C++之Deque容器初学(一)
- Android UI 初学笔记(一)
- StringBuffer,StringBuilder,数组常见操作,Arrays,基本类型包装类--2
- windows下,通过进程名杀死进程
- hdu 2844 coins
- 第一周第三天([算式][九九乘法表][求素数][求1000以内数相加但尾数是三的数跳出不计算][生小兔子问题] [金字塔问题] [求1+3+...+(2^20-1)的值][求1000以内的完全数])
- The Third Day
- Android初学之路(一)
- ThreadLocal类及常用的线程安全类探究
- POJ
- UVa136 priority_queue优先队列
- HashCode有多大可能重复?
- 对dispatch_async到主线程的逻辑封装成C/C++接口类型
- 交叉链表
- MyEclipse下复制的web项目
- 深入理解JDBC的超时设置