020-29133788
    资 讯
    您的位置:首页 >> 资 讯 >> 软件应用 >> 编程开发 >> 正文
    在Intent间传递2个不同元件的值

    点击:   发布日期:2013-07-17

    本文来自 www.020fix.com

    在Intent间传递2个不同元件的值,并且设定可以清除EditText


    主程式
    public class MainActivity extends Activity {
         private EditText name;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           
            Spinner spinner=(Spinner)findViewById(R.id.spinner);
            //建立一个Adapter物件来放置下拉式选单物件
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,new String[]{"国营","民营","股份有限公司","SOHO"});
           //选择下拉式选单样式
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
           
            //EditText
            name=(EditText)findViewById(R.id.username);
            //next Activity
            Button next_btn=(Button)findViewById(R.id.next);
            next_btn.setOnClickListener(ret_btn);
            //Button event
            Button button=(Button)findViewById(R.id.clear);
            button.setOnClickListener(bc);
           
           
        }
        //rec_btn
        private OnClickListener ret_btn=new OnClickListener(){
                        
                    @Override
                    public void onClick(View v) {
                            // TODO Auto-generated method stub
                            //page透过Intent通知系统框架
                            Intent intent=new Intent();
                            //指定要呼叫的Activity class
                            intent.setClass(MainActivity.this,Report.class);
                            //建立Bundle物件
                            Bundle bundle=new Bundle();
                            //将附加的讯息储存在Bundle中
                            bundle.putString("username",((EditText)findViewById( R.id.username)).getText().toString());
                            bundle.putString("spinner",((Spinner)findViewById(R.id.spinner)).getSelectedItem().toString());
                            //将Bundle指定到Intent
                            intent.putExtras(bundle);
                            //呼叫新的Activity
                            startActivity(intent);
                            // 不结束原先的 Activity Class
                // 这样按返回键时, 就可以回到这个 Activity 了
                //RadioActivity.this.finish();

                    }
               
        };
        private OnClickListener bc=new OnClickListener(){

                    @Override
                    public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                   name.setText("");
                    }
         };
           
    }

    下一个Intent
    public class Report extends Activity {
            private TextView name;
            /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.report);
           
            //TextView name
             name=(TextView)findViewById(R.id.show);
            
             //取得前一个Activity传过来的Bundle物件
             Bundle nameb = this.getIntent().getExtras();
             Bundle namec = this.getIntent().getExtras();
           // 将 取得的Bundle 中的资料设定
             String result = nameb.getString("username");
             String resultA = namec.getString("spinner");
           // 显示结果    
             name.setText("你的帐号是:"+result+",你的单位是:"+resultA);
       }
    }

    上一篇:Dialog 对话方块
    下一篇:播放音乐