`
7090
  • 浏览: 274857 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android Intent初步试用

阅读更多

Intent 的初步使用,用于切换Activity,同时传递一些参数到目的Activity

实现一个类似乘法的功能

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    //第一个乘数框
    EditText m1;
    //第二个乘数框
    EditText m2;
    //结果框
    EditText result;
    //计算按钮
    Button calu;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //从main.xml中得到定义的几个EditText
        m1=(EditText)this.findViewById(R.id.m1);
        m2=(EditText)this.findViewById(R.id.m2);
        result=(EditText)this.findViewById(R.id.result);
      //从main.xml中得到定义的几个Button
        calu=(Button)this.findViewById(R.id.calu);
        //给按钮添加Onclick事件注册监听
        calu.setOnClickListener(this);       
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(arg0==calu){
            String m1Text=m1.getText().toString();
            String m2Text=m2.getText().toString();
            int m1Num=Integer.parseInt(m1Text);
            int m2Num=Integer.parseInt(m2Text);
            String resultText=String.valueOf(m1Num*m2Num);
            result.setText(resultText);
            
            //实现一个传递的Intent
            Intent intent =new Intent();
            intent.putExtra("m1", m1Text);
            intent.putExtra("m2", m2Text);
            intent.setClass(this, ResultActivity.class);
            this.startActivity(intent);
        }
    }  
}

ResultActivity.java

public class ResultActivity extends Activity{
    
    public TextView result;
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.result);
        
        result=(TextView)this.findViewById(R.id.result);
        parseIntent();
    }
    public void parseIntent(){
        //得到传递的Intent
        Intent intent=this.getIntent();
        //得到传递的Intent的参数
        String m1Text=intent.getExtras().getString("m1");
        String m2Text=intent.getExtras().getString("m2");
        int m1Num=Integer.parseInt(m1Text);
        int m2Num=Integer.parseInt(m2Text);
        String resultText=String.valueOf(m1Num*m2Num);
        //对目标结果的显示
        result.setText(resultText);
        
    }
    
}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText android:id="@+id/m1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ></EditText>
    
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@+string/cul"
/>
<EditText android:id="@+id/m2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ></EditText>    
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@+string/equlas"
/>            
<EditText android:id="@+id/result"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ></EditText>
<Button     android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@+string/caluate"
            android:id="@+id/calu"
            > </Button>        
</LinearLayout>

 



 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics