计算器实例

package en.captain.edittext_and_button;

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 EditText_and_ButtonActivity extends Activity {
	private EditText edit_one;
	private EditText edit_two;
	private TextView view;  
	private Button button;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        edit_one = (EditText)findViewById(R.id.one_edit);
        edit_two =(EditText)findViewById(R.id.two_edit);
        button = (Button)findViewById(R.id.button);
        view = (TextView)findViewById(R.id.view);
        button.setOnClickListener(new buttonlisener());
        view.setText("乘");
        button.setText("计算");
    }
    class buttonlisener implements OnClickListener{

		public void onClick(View v) {
			// TODO Auto-generated method stub
			String sone = edit_one.getText().toString();//得到编辑框内容
		    String stwo = edit_two.getText().toString();
			Intent intent = new Intent();
			intent.putExtra("one_name",sone);//附加的内容名,参数。
			intent.putExtra("two_name",stwo);
			intent.setClass(EditText_and_ButtonActivity.this,Result.class);
			startActivity(intent);	
		}
    	
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {//菜单的定义  组,ID,排序,显示文字。
		// TODO Auto-generated method stub
		menu.add(0, 1, 1, "推出");
		menu.add(0, 2, 2, "关于");
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {//根据ID实现方法
		// TODO Auto-generated method stub
		if(item.getItemId() == 1)
			button.setText("菜单的退出被选择!");
		if (item.getItemId() == 2)
			button.setText("菜单的关于被选择!");
		return super.onOptionsItemSelected(item);
	} 
}

Result.java

 

package en.captain.edittext_and_button;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Result extends Activity {
	private TextView my_view;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		my_view = (TextView)findViewById(R.id.my_view);
		Intent intent = getIntent();//注意是getIntent()不是new Intent;
		String get_one = intent.getStringExtra("one_name");
		String get_two = intent.getStringExtra("two_name");
		int int_one = Integer.parseInt(get_one);//转换成整型
		int int_two = Integer.parseInt(get_two);
		int result_out = int_one * int_two;
		my_view.setText(result_out + "");//当整型和字符型相结合是 都变成字符型;
	}
}

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/one_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <EditText
	    android:id="@+id/two_edit"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"/>
    <Button
	    android:id="@+id/button"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"/>
</LinearLayout>

计算器实例

你可能感兴趣的:(计算器)