Android学习笔记3
(5)练习做一个实现两个数相乘的APP
①.java代码:
//MainActivity.java
package com.example.hello;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private TextView tv;
private EditText et_a;
private EditText et_b;
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
et_a=(EditText)findViewById(R.id.et_a);
et_b=(EditText)findViewById(R.id.et_b);
bt=(Button)findViewById(R.id.bt);
//bt.setText("计算");
//tv.setText("乘以(X)");
tv.setText(R.string.tv);
bt.setText(R.string.bt);
bt.setOnClickListener(new BtListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1,1 , R.string.menu_a); //add(当前组的id,菜单的id,当前菜单中小块排序的id,小块显示的值)
menu.add(0, 1,2 , R.string.menu_b);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id=item.getItemId();
if(id==1)
{
finish();
}
return super.onOptionsItemSelected(item);
}
class BtListener implements View.OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent=new Intent();
String et_astr=et_a.getText().toString();
String et_bstr=et_b.getText().toString();
intent.putExtra("et_a", et_astr);
intent.putExtra("et_b", et_bstr);
intent.setClass(MainActivity.this,Hello1Activity.class );
MainActivity.this.startActivity(intent);
}
}
}
//HelloActivity.java
package com.example.hello;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class Hello1Activity extends AppCompatActivity
{
private TextView tv_b;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello1);
tv_b=(TextView)findViewById(R.id.tv_b);
Intent intent=getIntent();
String et_astr=intent.getStringExtra("et_a");
String et_bstr=intent.getStringExtra("et_b");
int et_ain=Integer.getInteger(et_astr);
int et_bin=Integer.getInteger(et_bstr);
int result=et_ain*et_bin;
tv_b.setText(result+ " " ); //setText(必须是字符串类型)
}
}
//
②.xml代码:
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_a"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/et_b"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00f"
/>
</LinearLayout>
//activity_hello1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Hello1Activity">
<TextView
android:id="@+id/tv_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
//string.xml
<resources>
<string name="app_name">Hello</string>
<string name="HelloActivity">HelloActivity!</string>
<string name="tv">乘以(X)</string>
<string name="bt">计算</string>
<string name="menu_a">后退</string>
<string name="menu_b">关于</string>
</resources>
Android学习笔记3的更多相关文章
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
- 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...
- 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...
随机推荐
- THUSC 2021 游记
想了想不往博客园放不行,还是放上来了. 原文 \[\texttt{Brief Introduction} \] 众所周知,THUSC2021 5 月 15-16 日在杭州市 XJ 中学举办,然而由于 ...
- Git差异并列显示
默认的git diff命令只会将文件的修改差异使用"+","-"符号标注出来,并不直观. 最理想的方式应该是使用诸如"DiffMerge"这 ...
- C++之面试题(4)
题目描述 来源:牛客网 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素. 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在 ...
- cmake命令行生成32位和64位项目
概述 本文演示环境: win10 + VS2017 1.指定变量值 咱们常用命令[cmake ..]在build目录下配置生成项目和解决方案. 其实,这个命令还有其他用法. 指定CMakeLists. ...
- 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
- 【LeetCode】495. Teemo Attacking 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 如何利用Python实现Office在线预览
目前,市场对于Office在线预览功能的需求是很大的.对于我们用户本身来说,下载Office文件后再实现预览是极其不方便的,何况还有一些不能打开的专业文档.压缩文件等.此时,能提供在线预览服务的软件就 ...
- CS5211替代PS8625|设计EDP转LVDS转接屏方案|替代PS8625
PS8625将作为DP或eDP接收器设备出现在视频源中,并将作为LVDS显示面板的LVDS源设备.该设备是一个完全集成的解决方案,不需要外部CPU.内存.时钟基准或电压调节器.PS8625可配置为从显 ...
- Java基础(八)——IO流2_缓冲流、转换流
一.缓冲流 1.介绍 缓冲流:不能直接作用在文件上,需要包一层,它是一种处理流.用于提高文件的读写效率.它在流的基础上对流的功能进行了增强.提高读写速度的原因:内部提供了一个缓冲区.缺省使用 8192 ...