【问题标题】:passing values by buttons and count the whole thing通过按钮传递值并计算整个事情
【发布时间】:2014-02-01 08:07:52
【问题描述】:

我有 3 节课。在每节课中,我都有 2 个按钮,例如是和否。 我想通过 1 表示是,0 表示否。最后我想计算总数, 我的意思是,如果有人每次都按“是”,那么它应该是 3。 怎么做?`


通过按钮传递值并计算整个事件

    //this is 1st class
    package com.atlantis.learnactivities;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Main extends Activity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button b1 = (Button) findViewById(R.id.b1);
            b1.setOnClickListener (new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent(Main.this,Second.class));
                }
            });

            Button b2 = (Button) findViewById(R.id.b2);
            b2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent(Main.this,Second.class));
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

//这是第二类

    package com.atlantis.learnactivities;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Second extends Activity {
        String gender =null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);

            Button b3 = (Button) findViewById(R.id.b3);
            b3.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent(Second.this,Three.class));
                }
            });
            Button b4 = (Button) findViewById(R.id.b4);
            b4.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent(Second.this,Four.class));
                }
            });



        }
    }

【问题讨论】:

标签: android button android-activity onclicklistener


【解决方案1】:

使用 sharedPreference。 单击button 时添加一个整数值。

添加

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt("btn_value", "value");
editor.commit();

从共享偏好中检索数据

 SharedPreferences prefs = getPreferences(MODE_PRIVATE); 

 int value = prefs.getInt("btn_value", -1);

【讨论】:

    【解决方案2】:

    在你当前的 Activity 中,创建一个新的 Intent:

    Intent i = new Intent(Main.this, Second.class);
    i.putExtra("new_variable_name","value");
    startActivity(i);
    

    然后在第二个活动中,检索这些值:

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("new_variable_name");
    }
    

    您不需要使用 SharedPreferences 在同一个应用程序中传递数据。

    【讨论】:

      【解决方案3】:

      如果您不需要存储值并将其从一个类传递给下一个类,请使用 putExtra。
      例如。在您的点击侦听器中:

          intent.putExtra("value", 1);  
      //or intent.putExtra("value", "1") , depending on whether you want to use it as a String or int  
      

      在你传递值的类中,检索它

      this.getIntent().getExtras().getString("value");  
      //this.getIntent().getExtras().getInt("value");
      

      检查:
      http://pkhandroid.wordpress.com/2012/10/02/post9-intent-message-passing-within-activities/
      http://mobileorchard.com/android-app-development-using-intents-to-pass-data-and-return-results-between-activities/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-19
        • 2012-02-22
        • 2013-11-05
        • 2015-02-10
        • 1970-01-01
        相关资源
        最近更新 更多