安卓实训笔记

安卓开发就是用xml写界面,java代码控制逻辑。

xml在res的layout目录,java代码在src目录的包中。


安卓四大组件:Activity,Service,Content Provider,BroadcastReceiver。


首先学习界面布局以及常用控件。

LinearLayout和RelativeLayout

TextView,Button


orientation,gravity,align

margin,padding

layout_above/below/toLeftOf/toRightOf


练习:对联,八卦图


使用id作为唯一标识找到xml控件在内存中的对象

设置监听的几种方式:

1.(匿名)内部类

2.android:onClick="method_name"   (在MainActivity类中新建方法public void Method_name(View view){})


用Toast简单输出信息

Toast.makeText(getApplicationContext(), "massage_content", Toast.LENGTH_SHORT).show();


页面跳转和传值

Intent intent=new Intent(MainActivity.this,SecondActivity.class);    //SecendActivity记得在AndroidManifest.xml中注册

intent.putExtra("keyname", value);

如果传的数据多用bundle

Bundle bundle=new Bundle();
  bundle.putType("keyname",value);
intent.putExtras(bundle);

startActivity(intent);    //页面切换


目的页面代码:

Type value=getIntent().getTypeExtra("keyname",defaultValue);

或者

Bundle bundle=this.getIntent().getExtras();
Type=bundle.getType("keyname");


示例:计算BMI

activity_main.xml

<LinearLayout 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"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="20dp"
    tools:context="com.example.bmi.MainActivity" 
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="身高(cm)"/>
        <EditText
            android:id="@+id/height"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入身高"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="体重(kg)"/>
        <EditText
            android:id="@+id/weight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入体重"/>
    </LinearLayout>
    <Button
        android:id="@+id/calc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="计算"/>
    <Button
        android:id="@+id/reset"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onReset"
        android:text="重置"/>
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
	    <TextView
	        android:id="@+id/content"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:textSize="24sp"
	        android:scrollbars="vertical"
	        android:text="@string/content"/>
    </ScrollView>
</LinearLayout>

result.xml

<?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" >
    <ImageView 
        android:id="@+id/dscpic"
        android:layout_width="120dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:background="#000000"/>
    <TextView
        android:id="@+id/index"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="健康指数:"/>
    <TextView
        android:id="@+id/condition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:text="健康状况:"/>
    <TextView
        android:id="@+id/normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:text="正常体重:"/>

</LinearLayout>

MainActivity.java

package com.example.bmi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	//private EditText et_height=null;
	//private EditText et_weight=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		   final EditText et_height=(EditText) findViewById(R.id.height);
		   final EditText et_weight=(EditText) findViewById(R.id.weight);
		   final Button btn_calc=(Button) findViewById(R.id.calc);
		 //final Button btn_reset=(Button) findViewById(R.id.reset);
		
		btn_calc.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//bmi=kg/m^2
				//float height=et_height.get
				//setContentView(R.layout.result);
				String height_str=et_height.getText().toString().trim();
				String weight_str=et_weight.getText().toString().trim();
				
				if(height_str==null || height_str.equals("")){
					Toast.makeText(getApplicationContext(), "height can't be empty!", Toast.LENGTH_SHORT).show();
					return;
				}
				if(TextUtils.isEmpty(weight_str)){
					Toast.makeText(getApplicationContext(), "weight can't be empty!", Toast.LENGTH_SHORT).show();
					return;
				}
				try{
					float bmi=Float.valueOf(weight_str)/((Float.valueOf(height_str)/100)*(Float.valueOf(height_str)/100));
					Toast.makeText(getApplicationContext(), String.valueOf(bmi), Toast.LENGTH_SHORT).show();
					Intent intent=new Intent(MainActivity.this,ResultActivity.class); 
					Bundle bundle=new Bundle();
					bundle.putFloat("bmi",bmi);
					intent.putExtras(bundle);
					
					
					//intent.putExtra("bmi", bmi);
					startActivity(intent);
					 
					
					
					
					
				}catch(NumberFormatException e){
					Toast.makeText(getApplicationContext(), "inputs illegal !!", Toast.LENGTH_SHORT).show();
				}
				
				
			}
		});
	}
	//第三种方式设置监听方法
	public void onReset(View view){
		//
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

ResultActivity.java

package com.example.bmi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		/*Bundle bundle=this.getIntent().getExtras();
		float bmi=bundle.getFloat("bmi");
		*/
		float bmi=getIntent().getFloatExtra("bmi", -1);
		TextView tv_index=(TextView) findViewById(R.id.index);
		tv_index.setText("健康指数:"+String.valueOf(bmi));
		
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值