Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.0 kB
1
Indexable
Never
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edtID"
        android:hint="ID"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/edtName"
        android:hint="Name"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/edtSDT"
        android:hint="Dien thoai"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/edtDiachi"
        android:hint="Diachi"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/edtEmail"
        android:hint="Email"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="SAVE"
        android:textSize="15sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="UPDATE"
        android:textSize="15sp"
        android:textStyle="bold" />

   <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@+id/btnSave" />



MainActivity.java
public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayAdapter <String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] items = {"Item 1","Item 2","Item 3","Item 4","Item 5"};
        adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
        listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.example_menu, menu);
        //Để lấy ra item Thêm ta sử dụng
        MenuItem itemThem = menu.findItem(R.id.item1);
        //Lấy ra SearchView của Item thêm
        SearchView mysearchview = (SearchView) MenuItemCompat.getActionView(itemThem);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater= getMenuInflater();
       if(v.getId() == R.id.listView){
            inflater.inflate(R.menu.example_menu,menu);
        }
    }

    //Bắt sự kiện người dùng click chuột vào example_menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Khai báo lấy íd control click trên layout
        int id = item.getItemId();
        switch (id) {
            case R.id.item1:
                Toast.makeText(MainActivity.this, "Thêm", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item2:
                Toast.makeText(MainActivity.this, "Sửa", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item3:
                Toast.makeText(MainActivity.this, "Xoá", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item4:
//                Toast.makeText(MainActivity.this, "Thoát", Toast.LENGTH_SHORT).show();
                //Đóng chương trình
                finish();
        }
        return super.onOptionsItemSelected(item);
    }