Android meun
package com.example.appdemo
import android.app.ActionBar
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*
import android.widget.*
import android.widget.SeekBar.*
class MainActivity : AppCompatActivity(){
private val btn1:Button by lazy {findViewById<Button>(R.id.btn1)}
private val btn2:Button by lazy {findViewById<Button>(R.id.btn2)}
private val linerLayout:LinearLayout by lazy {findViewById<LinearLayout>(R.id.linerLayout)}
private val myToast:Toast by lazy {Toast(this)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//menuInflater(R.menu.setting,)
registerForContextMenu(linerLayout)
btn1.setOnClickListener{
startActivity(Intent(this,MainActivity2::class.java))
}
btn2.setOnClickListener{
val popupMenu=PopupMenu(this,btn2)
popupMenu.menuInflater.inflate(R.menu.context,popupMenu.menu)
popupMenu.setOnMenuItemClickListener(object:PopupMenu.OnMenuItemClickListener{
override fun onMenuItemClick(p0: MenuItem?): Boolean {
when(p0?.itemId){
R.id.copy->myToast.setText("copy")
R.id.cut->myToast.setText("cut")
R.id.tie->myToast.setText("tie")
}
myToast.show()
return true
}
})
popupMenu.show()
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.setting,menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.save->myToast.setText("save")
R.id.setting->myToast.setText("setting")
R.id.share->myToast.setText("share")
R.id.more->myToast.setText("more")
}
myToast.show()
return super.onOptionsItemSelected(item)
}
override fun onCreateContextMenu(
menu: ContextMenu?,
v: View?,
menuInfo: ContextMenu.ContextMenuInfo?
) {
//方法一 代码实现 add(第一个参数组id 第二个item id 第三个item排序id
menu?.add(1,1,1,"copy")
menu?.add(1,2,2,"cut")
menu?.add(1,3,3,"paste")
val addSubMenu = menu?.addSubMenu(1, 4, 4, "more")
addSubMenu?.add(2,5,1,"linked")
addSubMenu?.add(2,6,2,"share")
//方法二 xml
// menuInflater.inflate(R.menu.context,menu)
super.onCreateContextMenu(menu, v, menuInfo)
}
override fun onContextItemSelected(item: MenuItem): Boolean {
when(item.itemId){
1->myToast.setText("copy")
2->myToast.setText("cut")
3->myToast.setText("paste")
}
// when(item.itemId){
// R.id.copy->myToast.setText("copy")
// R.id.cut->myToast.setText("cut")
// R.id.tie->myToast.setText("tie")
// }
myToast.show()
return super.onContextItemSelected(item)
}
}
package com.example.appdemo import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.ActionMode import android.view.Menu import android.view.MenuItem import android.widget.EditText import android.widget.TextView import android.widget.Toast class MainActivity2 : AppCompatActivity(),ActionMode.Callback { private val txt: TextView by lazy { findViewById<TextView>(R.id.txt) } private val myToast: Toast by lazy { Toast(this) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) txt.setOnClickListener { startActivity(Intent(this,MainActivity::class.java)) } startActionMode(this) } override fun onActionItemClicked(p0: ActionMode?, p1: MenuItem?): Boolean { when(p1?.itemId){ R.id.copy->myToast.setText("copy") R.id.cut->myToast.setText("cut") R.id.tie->myToast.setText("tie") } myToast.show() return true } override fun onCreateActionMode(p0: ActionMode?, p1: Menu?): Boolean { menuInflater.inflate(R.menu.context,p1) return true } override fun onPrepareActionMode(p0: ActionMode?, p1: Menu?): Boolean { Log.e("Tag","prepare") return false; } override fun onDestroyActionMode(p0: ActionMode?) { Log.e("Tag","onDestroyActionMode") } }