当前位置:操作系统 > 安卓/Android >>

android基础(Activity)

package ahu.edu; 
 
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 Activity03Activity extends Activity { 
     
 
     
    private EditText factorOne; 
    private EditText factorTwo; 
    private TextView symbol; 
    private Button calculate; 
 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        factorOne = (EditText)findViewById(R.id.factorOne); 
        factorTwo = (EditText)findViewById(R.id.factorTwo); 
        symbol = (TextView)findViewById(R.id.symbol); 
        calculate = (Button)findViewById(R.id.calculate); 
        symbol.setText(R.string.symbol); 
        calculate.setText(R.string.calculate); 
        calculate.setOnClickListener(new CalcualteListener()); 
    } 
     
    class CalcualteListener implements OnClickListener { 
        public void onClick(View v) { 
            String factorOneStr = factorOne.getText().toString(); 
            String factorTwoStr = factorTwo.getText().toString(); 
            Intent intent = new Intent(); 
            intent.putExtra("one", factorOneStr); 
            intent.putExtra("two", factorTwoStr); 
            intent.setClass(Activity03Activity.this,ResualActivity.class); 
            Activity03Activity.this.startActivity(intent); 
        } 
    } 
     
    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();  //----比较柔和的方式 
            System.exit(0); //---残酷的方式 
        } 
        return super.onOptionsItemSelected(item); 
    } 
 

[java]
package ahu.edu; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class ResualActivity extends Activity{ 
    private TextView resultView; 
     
     
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.result); 
        resultView = (TextView)findViewById(R.id.result); 
        Intent intent = getIntent(); 
        String factorOneString = intent.getStringExtra("one"); 
        String factorTwoString = intent.getStringExtra("two"); 
        int factorOneInt = Integer.parseInt(factorOneString); 
        int factorTwoInt = Integer.parseInt(factorTwoString); 
        int result = factorOneInt * factorTwoInt; 
        resultView.setText(result + ""); 
    } 

[html]
<?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" > 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
    <EditText  
        android:id="@+id/factorOne" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:hint="@string/message"  
        android:inputType="none"/> 
    <TextView  
        android:id="@+id/symbol" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text=""/> 
    <EditText  
        android:id="@+id/factorTwo" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:hint="@string/message"  
   &nbs

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,