android点击事件
2019-08-19 本文已影响0人
栗子daisy
MainActivity.java
package com.example.tmftest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏标题栏
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.btn_menu);
Button btn2 = findViewById(R.id.btn_calendar);
Button btn3 = findViewById(R.id.btn_maintenance);
btn1.setOnClickListener(MainActivity.this);
btn2.setOnClickListener(MainActivity.this);
btn3.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_menu:
Toast.makeText(MainActivity.this, "btn_menu", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_calendar:
Toast.makeText(MainActivity.this, "btn_calendar", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_maintenance:
Toast.makeText(MainActivity.this, "btn_maintenance", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_menu"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="菜品维护"
/>
<Button
android:id="@+id/btn_calendar"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="菜单日历"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_maintenance"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="参数维护"
/>
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="+"
/>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>