如何使用onClick方法,使用switch创建

时间:2015-09-30 21:06:27

标签: android

你们有没有想法我如何使用这种设置onClick动作的方式。我真的不想为onCreate方法中的每个按钮创建一个对象,即使它并没有减慢应用程序的速度,但我仍然想编写一个可靠的代码。谢谢你的帮助!

private void onClick(View v) {
    switch (v.getId()) {
        case R.id.houseButton:
            Log.d("onClick","Housebutton worked");
            houseNumber += 1;
            saveToFile();

            break;
        case R.id.penthouseButton:
            Log.d("onClick","Penthouse worked");
            penthouseNumber += 1;
            saveToFile();
            break;
        case R.id.ferrariButton:
            Log.d("onClick","Ferrari worked");
            ferrariHumber += 1;
            saveToFile();
            break;
        case R.id.goBackButton:
            Log.d("onClick","goBackButton worked");
            Intent intent = new Intent(BuyListActivity.this,MainActivity.class);
            startActivity(intent);
            break;
    }
}

2 个答案:

答案 0 :(得分:1)

这是你可以做到的:

  public class SampleActivity extends AppCompatActivity implements View.OnClickListener{
        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sample);
            Log.d("onCreateBuyList", "Started");

            Button buyHouseButton = (Button) findViewById(R.id.houseButton);
            Button buyPenthouseButton = (Button) findViewById(R.id.penthouseButton);
            Button buyFerrariButton = (Button) findViewById(R.id.ferrariButton);
            Button goBackButton = (Button) findViewById(R.id.goBackButton);
            buyHouseButton.setOnClickListener(this);
            buyHouseButton.setOnClickListener(this);
            buyHouseButton.setOnClickListener(this);
            goBackButton.setOnClickListener(this);

        }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
    case R.id.houseButton:
        Log.d("onClick","Housebutton worked");
        houseNumber += 1;
        saveToFile();

        break;
    case R.id.penthouseButton:
        Log.d("onClick","Penthouse worked");
        penthouseNumber += 1;
        saveToFile();
        break;
    case R.id.ferrariButton:
        Log.d("onClick","Ferrari worked");
        ferrariHumber += 1;
        saveToFile();
        break;
    case R.id.goBackButton:
        Log.d("onClick","goBackButton worked");
        Intent intent = new Intent(BuyListActivity.this,MainActivity.class);
        startActivity(intent);
        break;
}
        }
    }

答案 1 :(得分:0)

在您的类中实现onClickListener并设置方法setOnClickListener(this)this表示该类的侦听器。

类似这样的事情

public class someclass  extends Actitivy implements OnClickListener{
...
Button button1 = findViewById(R.id.houseButtoon);
Button button2 = findViewById(R.id.penthouseButton);
Button button3 = findViewById(R.id.ferrariButton);
Button button4 = findViewById(R.id.goBackButton);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
...

}



@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.houseButton:
        Log.d("onClick","Housebutton worked");
        houseNumber += 1;
        saveToFile();

        break;
    case R.id.penthouseButton:
        Log.d("onClick","Penthouse worked");
        penthouseNumber += 1;
        saveToFile();
        break;
    case R.id.ferrariButton:
        Log.d("onClick","Ferrari worked");
        ferrariHumber += 1;
        saveToFile();
        break;
    case R.id.goBackButton:
        Log.d("onClick","goBackButton worked");
        Intent intent = new Intent(BuyListActivity.this,MainActivity.class);
        startActivity(intent);
        break;
}


}