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 ...
随机推荐
- LuoguP7859 [COCI2015-2016#2] GEPPETTO 题解
Content 有 \(n\) 个数 \(1\sim n\).你需要在其中选若干个数.但是还有 \(m\) 个限制,第 \(i\) 个限制格式为 \(a_i\) 不能和 \(b_i\) 一起选.问你一 ...
- CF102B Sum of Digits 题解
Content 给定一个数 \(n\),每次操作可以将 \(n\) 变成 \(n\) 各位数之和.问你几次操作之后可以将 \(n\) 变为一位数. 数据范围:\(1\leqslant n\leqsla ...
- TURN协议(RFC5766详解)
如果一台主机处于NAT后面,那么在一定条件下两台主机无法之间进行通讯.在这种条件下,那么使用中继服务提供通讯是有必要的. 这个规范定义了一个名为TURN(使用中继穿越NAT)的协议,它允许一台主机使用 ...
- Learning to Sample
此处主要提出几个疑问和想法: 疑问: 为什么需要这个匹配过程?虽然G可能不是P的子集,但是为什么一定需要他是子集呢? 如果一定要匹配的话,匹配过程是没法反向传播的,所以只可以在推理阶段使用,那么这个推 ...
- Linux使用docker安装zimg图片服务器
官方地址:http://zimg.buaa.us/ 配置文件 zimg.lua --zimg server config --server config --是否后台运行 is_daemon = 1 ...
- byte数组(byte[])与MultipartFile相互转化
MultipartFile转化为byte数组 byte[] bytes= file.getBytes(); byte数组转化为MultipartFile <dependency> < ...
- 【LeetCode】1470. 重新排列数组 Shuffle the Array (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode-cn ...
- light oj -1245 - Harmonic Number (II)
先举个例子,假如给你的数是100的话,将100/2=50;是不是就是100除100-51之间的数取整为1: 100/3=33;100除50到34之间的数为2,那么这样下去到sqrt(100);就可以求 ...
- 图的存储:邻接矩阵(C++)
1. 演示 无向图: 有向网: 2. 代码 1 #include <iostream> 2 #include <unordered_map> 3 #include <ve ...
- 一、SQL高级语句
摘抄别的博主的博客主要总去CSDN看不太方便自己整理一下加深记忆! 导入文件至数据库 #将脚本导入 source 加文件路径 mysql> source /backup/test.sql; se ...