如何在同一按钮上切换执行指令?

时间:2018-04-25 16:19:43

标签: java android

请帮忙 我想为同一个按钮放几个执行命令 我的意思是当我第一次按下按钮做某件事 当第二次按下时再做一次,依此类推十次压力 我尝试了这段代码,但它无法正常工作

function UseMapFunction(str) {
     var strArr = str.toLowerCase().split(' ');
     
     var newStrArr = strArr.map(function(val){
      return val.replace(val.charAt(0),val.charAt(0).toUpperCase());
     });
     console.log(newStrArr.join(' '));
}
UseMapFunction("one way to do it is with map");

function UseSubstring(str) {
     var strArr = str.split(' ');
     var newArr =[];
     for (var i = 0; i<strArr.length; i++){
       newArr.push(strArr[i].substring(0,1).toUpperCase() + strArr[i].substring(1,strArr[i].length).toLowerCase());
     }
  console.log(newArr.join(' '));
}
UseSubstring("alternatively use substring and it works fine too");

他总是做第一个条件而忽略了第二个条件 我在Stack OverFlow网站上阅读了这段代码

public class ChickenBlanche extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
Button b1;
int i=0;


b1=(Button) findViewById(R.id.btns);

    b1.setOnClickListener( new View.OnClickListener() {
        @SuppressLint("WrongConstant")
        @Override
        public void onClick(View view) {

            if(i == 0)
            {
                Intent in = new Intent();
                in.putExtra( REG_INT_TAG, quantity1);
                setResult(RESULT_OK,in);
                finish();

            }
            else if(i==1)
            {
                Intent in = new Intent();
                in.putExtra( REG_INT_TAG1, quantity1);
                setResult(RESULT_OK,in);
                finish();
            }
        }
    } );

但我真的不知道如何将它应用到我的按钮

2 个答案:

答案 0 :(得分:0)

尝试以下示例:

1)Demo4.class:------------

public class Demo4 extends AppCompatActivity {

private Button b;
public static final String WHICH = "which";
private SharedPreferences p;
public static final String REG_INT_TAG = "reg_int_tag";

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

    p = getApplicationContext().getSharedPreferences("p_key",
            0);

    b = (Button) findViewById(R.id.b);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (p != null) {
                switch (p.getInt(WHICH, 0)) {
                    case 0:
                        result("A");
                        break;
                    case 1:
                        result("B");
                        break;
                    case 2:
                        result("C");
                        break;
                    case 3:
                        result("D");
                        break;
                    case 4:
                        result("E");
                        break;
                    case 5:
                        result("F");
                        break;
                    case 6:
                        result("G");
                        break;
                    case 7:
                        result("H");
                        break;
                    case 8:
                        result("I");
                        break;
                    case 9:
                        result("J");
                        break;
                    default:
                        result("A");
                        break;
                }
            }
        }
    });
}

private void result(String value){

    Intent in = new Intent();
    in.putExtra(REG_INT_TAG,value);
    setResult(RESULT_OK,in);
    finish();
}

}

2)mainActivity.class:-------

public class mainActivity extends AppCompatActivity {

    private Button b2;
    private SharedPreferences p;
    private final int ACTIVITY_2 = 12345;

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

        p = getApplicationContext().getSharedPreferences("p_key",
                0);

        b2 = (Button) findViewById(R.id.b2);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(mainActivity.this , Demo4.class);
                startActivityForResult(i , ACTIVITY_2);
            }
        });
    }

    public void setInt(String Name, int value){
        if(p != null){
            SharedPreferences.Editor editor = p.edit();
            editor.putInt(Name, value);
            editor.apply();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ACTIVITY_2){
            if(resultCode == RESULT_OK){
                if(data != null){
                    Toast.makeText(getApplicationContext() , data.getStringExtra(Demo4.REG_INT_TAG) , Toast.LENGTH_LONG).show();
                    updateWhich();
                }
            }
        }
    }


private void updateWhich(){

    if(p != null){
        int which = p.getInt(Demo4.WHICH , 0);
        setInt(Demo4.WHICH, (which >= 0 && which < 9) ? which + 1 : 0);
        // if which is between 0 including and 9 excluding increment which otherwise which = 0.
    }
}

}

3)activity_2.xml:--------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click"
    android:id="@+id/b"/>

</android.support.constraint.ConstraintLayout>

4)activity_main.xml:--------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="open 2"
    android:id="@+id/b2"/>

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

按下按钮时每次增加i值。 看看代码

b1=(Button) findViewById(R.id.btns);

b1.setOnClickListener( new View.OnClickListener() {
    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View view) {

        if(i == 0)
        {
            Intent in = new Intent();
            in.putExtra( REG_INT_TAG, quantity1);
            setResult(RESULT_OK,in);
            finish();

        }
        else if(i==1)
        {
            Intent in = new Intent();
            in.putExtra( REG_INT_TAG1, quantity1);
            setResult(RESULT_OK,in);
            finish();
        }

        i=i+1;//increment i value on button click at every time.

         if(i=>maxValue){//If i value reach maximum point then should reset also
              i=0;
             }      
    }
} );