OptionMenu ContextMenu 实现长按弹出和点击弹出
OptionMenu ContextMenu 实现长按弹出和点击弹出package com.example.mvvm;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.ContextMenu;import android.view.Menu;impor
·
OptionMenu ContextMenu 实现长按弹出和点击弹出
package com.example.mvvm;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button btn;
private TextView tvresute;
private Button menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
tvresute =findViewById(R.id.tvresute);
menu = findViewById(R.id.menu);
menu.setOnCreateContextMenuListener(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setText("下载中....");
Toast.makeText(MainActivity.this,"下载中",Toast.LENGTH_SHORT).show();
}
});
tvresute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num = "输出值";
Intent intent = new Intent(MainActivity.this, Resute.class);
intent.putExtra("MSG",num);
startActivity(intent);
}
});
// menu.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Log.d("输出","d");
// }
// });
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu,v,menuInfo);
menu.add(0,2,0,"添加1");
menu.add(0,3,0,"删除");
}
@Override
public boolean onContextItemSelected(MenuItem item){
switch (item.getItemId()){
case 2:
Log.d("添加","成功");
break;
case 3:
Log.d("删除","成功");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
// menu.add(0,2,0,"添加1");
// menu.add(0,3,0,"删除");
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_mian,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.add:
Log.d("添加","成功");
break;
case R.id.add2:
Log.d("删除","成功");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
更多推荐
已为社区贡献8条内容
所有评论(0)