Android常用空間学習---乗算計算機
6168 ワード
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.widget.TextView;
//import android.view.View.OnClickListener;
//import android.text.Editable;
//import android.text.TextWatcher;
import android.widget.EditText;
public class MainActivity extends Activity
{
private EditText stinga;
private EditText stingb;
private TextView symble;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stinga = (EditText)findViewById(R.id.Texta);
stingb = (EditText)findViewById(R.id.Textb);
symble = (TextView)findViewById(R.id.textView1);
button = (Button)findViewById(R.id.button1);
symble.setText(R.string.chengyi);
button.setText(R.string.jisuan);
//
/* butto.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
String factorStra = stinga.getText().toString();
String factorStrb = stingb.getText().toString();
Intent intent =new Intent();
intent.putExtra("one",factorStra );
intent.putExtra("two",factorStrb );
intent.setClass(MainActivity.this, TestActivity.class);
MainActivity.this.startActivity(intent);
}
});
*/
}
public void myClickHandler(View target){
//setTitle("myClickHandler01");
String factorStra = stinga.getText().toString();
String factorStrb = stingb.getText().toString();
Intent intent =new Intent();
intent.putExtra("one",factorStra );
intent.putExtra("two",factorStrb );
intent.setClass(MainActivity.this, TestActivity.class);
MainActivity.this.startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0, 1, 1, R.string.exit);
menu.add(0, 2, 2, R.string.about);
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1)
{finish();}
return super.onOptionsItemSelected(item);
}
}
メインインタフェースレイアウト<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:src="@drawable/logo" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Textb"
android:layout_alignRight="@+id/Textb"
android:layout_below="@+id/Textb"
android:layout_marginTop="68dp"
android:onClick="myClickHandler"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Texta"
android:layout_alignRight="@+id/Texta"
android:layout_below="@+id/Texta"
android:layout_marginTop="69dp"/>
<EditText
android:id="@+id/Texta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/logo"
android:layout_alignRight="@+id/logo"
android:layout_below="@+id/logo" />
<EditText
android:id="@+id/Textb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1" />
/>
</RelativeLayout>
計算結果ページソース:package com.example.androidtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Intent;
public class TestActivity extends Activity {
private TextView mytextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
mytextview = (TextView)findViewById(R.id.mytextView);
Intent intent =new Intent();
intent = getIntent();
String factorone = intent.getStringExtra("one");
String factortwo = intent.getStringExtra("two");
int factoroneint = Integer.parseInt(factorone);
int factortwoint = Integer.parseInt(factortwo);
int result = factoroneint * factortwoint;
mytextview.setText(result + "");
}
}