Button设定点击事件

Button设定点击事件

先定义一个Button变量,然后在OnCreate中找到它,即add_new_order =findViewById(R.id.add_new),然后设定点击事件即可。

public class MainActivity extends AppCompatActivity {

    private String TAG="CADCAD";
    private Button setting,add_new_order,search_order;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG,"-----------------new------------------");
        setting =findViewById(R.id.setting);
        add_new_order =findViewById(R.id.add_new);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });
        add_new_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add_new_order();
            }
        });
    }
}
posted @ 2020-04-25 20:51  caoanda  阅读(456)  评论(0编辑  收藏  举报