Android初学之路(一)

本文介绍了作者学习Android应用开发的初步实践,包括Activity的生命周期、控件操作及Intent使用等基本概念,并通过实例展示了如何创建简单的应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前要做单片机小项目——无线点阵广告牌!涉及到编写一个简单安卓软件......所以只有一点儿基础的我开始学习安卓了!第一天,当然学习一些简单了!主要是几个控件和监听的写法,还有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);
			}
		});
		
	}
}

<?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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值