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 ...
随机推荐
- Java 中的5个代码性能提升技巧,最高提升近10倍
文章持续更新,可以关注公众号程序猿阿朗或访问未读代码博客. 本文 Github.com/niumoo/JavaNotes 已经收录,欢迎Star. 这篇文章介绍几个 Java 开发中可以进行性能优化的 ...
- 10-2 bonding
创建bonding设备的配置文件 centos8 /etc/sysconfig/network-scripts/ifcfg-bond0 NAME=bond0 TYPE=bond DEVICE=bond ...
- Linux使用docker部署nacos
官网地址:https://nacos.io/zh-cn/docs/quick-start-docker.html 先把sql文件导入到mysql中 我也放了基础的sql /* * Copyright ...
- JAVA执行cmd命令方法
package com.cmd; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStre ...
- 【LeetCode】935. Knight Dialer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...
- 『学了就忘』vim编辑器基础 — 96、末行模式中的相关命令
目录 1.在文档中显示行号 2.是否显示文档内容相关颜色 3.是否将查找的字符串高亮显示 4.是否显示右下角的状态栏 5.是否在左下角显示如"--INSERT--"之类的状态栏 6 ...
- HDU 6608:Fansblog(威尔逊定理)
Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- C# RabbitMQ的使用
本文目的如题. 安装 先说一下RabbitMQ的安装,建议使用Docker镜像安装,Docker安装的好处是不管Windows系统还是Linux,安装步骤少,安装方法相同,不容易出错.使用下面的命令就 ...
- [JNI开发]使用javah命令生成.h的头文件
第一步:进入对应的.java目录 javac xxx.java 生成对应的xxx.class文件 第二步:退回到/java目录 javah -classpath . -jni 包名.类名
- Oracle 系统函数
函数名称 返回值类型 说明 示例 字符串函数 ascii(str) number 返回str首字母的ASCII码 select ascii('A') from dual; --65select a ...