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>

原文地址:https://www.cnblogs.com/lx06/p/14907851.html