職涯在走,鐵人賽文章一定要有。
大家好今天我要來繼續示範其他的menu,一開始我先來示範contextmenu,ContextMenu長按選單顧名思義就是點擊螢幕的某個地方兩三秒後會彈出選項來給使用者選取,廢話不多說那我們就開始吧!
一開始一樣先上程式碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:id="@+id/mTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請長按我"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.07" />
<ImageView
android:id="@+id/mImageView"
android:layout_width="300dp"
android:layout_height="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在佈局方面我給他一個TextView和一個ImageView,當使用者長按TextView後會彈出七大洲的選項給使用者選取,選取完後會將選到的洲的圖顯示到ImageView。
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private ImageView mImageView;
final private int north_america = 0;
final private int south_america = 1;
final private int asia = 2;
final private int europe = 3;
final private int africa = 4;
final private int oceania = 5;
final private int antarctica = 6;
String [] continent = {"北美洲","南美洲","亞洲","歐洲","非洲","大洋洲","南極洲"};
int[] continent_image = {R.drawable.north_america,R.drawable.south_america,R.drawable.asia,R.drawable.europe
,R.drawable.africa,R.drawable.oceania,R.drawable.antarctica
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.mTextView);
mImageView = findViewById(R.id.mImageView);
registerForContextMenu(mTextView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(1, north_america,1,"北美洲");
menu.add(1, south_america,2,"南美洲");
menu.add(1, asia,3,"亞洲");
menu.add(1, europe,4,"歐洲");
menu.add(1, africa,5,"非洲");
menu.add(1, oceania,6,"大洋洲");
menu.add(1, antarctica,7,"南極洲");
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case north_america:
mImageView.setImageResource(continent_image[0]);
break;
case south_america:
mImageView.setImageResource(continent_image[1]);
break;
case asia:
mImageView.setImageResource(continent_image[2]);
break;
case europe:
mImageView.setImageResource(continent_image[3]);
break;
case africa:
mImageView.setImageResource(continent_image[4]);
break;
case oceania:
mImageView.setImageResource(continent_image[5]);
break;
case antarctica:
mImageView.setImageResource(continent_image[6]);
break;
}
return super.onContextItemSelected(item);
}
}
我來介紹一下長按選單會用到的方法:
這樣就完成一個長按選單了,那今天的示範就到這了,謝謝大家的觀看。