今天给大家实现一个控件功能,具体见图
<a href="http://curran.blog.51cto.com/attachment/201103/3/2788306_1299163448OvNL.png"></a>
<a href="http://curran.blog.51cto.com/attachment/201103/3/2788306_1299163449qo3E.png"></a>
1:设置layout/main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_;fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_;fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edt1"
/>
<TextView
android:text="@string/tv"
android:id="@+id/tv"
android:id="@+id/edt2"
<Button
android:id="@+id/button"
android:text="@string/button"
</LinearLayout>
2:创建计算返回结果的布局layout/result.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_;fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/result"
android:layout_;fill_parent"
android:layout_height="wrap_content"
/>
3:填充布局所对应的values/string.xml
<resources>
<string name="hello">Hello World, ActivityDemo!</string>
<string name="app_name">ActivityDemo</string>
<string name="button">计算</string>
<string name="tv">乘以</string>
<string name="exit">退出</string>
<string name="about">关于</string>
</resources>
4:实现布局功能的ActivityDemo.java代码
package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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.TextView;
public class ActivityDemo extends Activity {
private EditText factorOne, factorTwo;
private TextView symbol;
private Button calculate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据控件的ID来取得代表控件的对象
factorOne = (EditText)findViewById(R.id.edt1);
factorTwo = (EditText)findViewById(R.id.edt2);
symbol = (TextView)findViewById(R.id.tv);
calculate = (Button)findViewById(R.id.button);
//将监听器的对象绑定到按钮对象上面
calculate.setOnClickListener(new CalculateListener());
}
//当客户点击MENU按钮的时候,调用该方法
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.exit);
menu.add(0, 2, 2, R.string.about);
return super.onCreateOptionsMenu(menu);
//当客户点击菜单当中的某一个选项时,会调用该方法
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1) {
finish();
}else if(item.getItemId() == 2) {
}
return super.onOptionsItemSelected(item);
}
class CalculateListener implements OnClickListener {
public void onClick(View v) {
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
//封装起来,将这俩个值放到Intent对象中
Intent intent = new Intent();
intent.putExtra("one", factorOneStr);
intent.putExtra("two", factorTwoStr);
intent.setClass(ActivityDemo.this, ResultActivity.class);
ActivityDemo.this.startActivity(intent);
}
5:实现转换为计算结果的布局ResultActivity.java代码
public class ResultActivity extends Activity {
private TextView resultView;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.result);
resultView = (TextView)findViewById(R.id.result);
Intent intent = getIntent();
String factorOneStr = intent.getStringExtra("one");
String factorTwoStr = intent.getStringExtra("two");
//转换int类型
int factorOneInt = Integer.parseInt(factorOneStr);
int factorTwoInt = Integer.parseInt(factorTwoStr);
int result = factorOneInt * factorTwoInt;
resultView.setText(result + "");
6:必须在AndroidManifest.xml注册ResultActivity.java
<activity android:name=".ResultActivity">
</activity>
本文转自 Art_Hero 51CTO博客,原文链接:http://blog.51cto.com/curran/506006,如需转载请自行联系原作者