3种Menu

2018-09-25  本文已影响11人  _弓长_大人

package com.example.bluegrey.app_layout;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private int count=0;
private SoundPool sp=new SoundPool(5, AudioManager.STREAM_MUSIC,0);
private int soundID;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);

    Button button=(Button)findViewById(R.id.card_button);

    soundID=sp.load(this,R.raw.meow,1);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Button button=(Button)v;
            if(button.getText().toString().equals("")){

                button.setBackgroundResource(R.drawable.blankcard);
                button.setText("A♣️");
                count++;
                TextView textView=(TextView)findViewById(R.id.count_tv);
                textView.setText("Count:"+count);

            }else{

                sp.play(soundID,1.0f,1.0f,0,0,1.0f);

                button.setBackgroundResource(R.drawable.stanfordtree);
                button.setText("");
            }

        }
    });


    final TextView textView=(TextView) findViewById(R.id.textViewii);


    // 长按
    registerForContextMenu(textView);



    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            PopupMenu popupMenu=new PopupMenu(MainActivity.this,v);
            popupMenu.inflate(R.menu.main_menu);
            popupMenu.show();



            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {


                    switch (item.getItemId()){
                        case R.id.upload:
                            Toast.makeText(MainActivity.this,"popup " +getString(R.string.upload),Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.download:
                            Toast.makeText(MainActivity.this,"popup " +getString(R.string.download),Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.stop:
                            Toast.makeText(MainActivity.this,"popup " +getString(R.string.stop),Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.quit:
                            Toast.makeText(MainActivity.this,"popup " +getString(R.string.quit),Toast.LENGTH_SHORT).show();
                            break;


                    }
                    return  true;
                }
            });
        }
    });

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

    getMenuInflater().inflate(R.menu.main_menu,menu);

    super.onCreateContextMenu(menu,v,menuInfo);



}

@Override
public boolean onContextItemSelected(MenuItem item) {

    switch (item.getItemId()){
    /*    case 1:
            Toast.makeText(this,"options upload",Toast.LENGTH_SHORT).show();
            break;
        case 2:
            Toast.makeText(this, "options downloud", Toast.LENGTH_SHORT).show();
            break;
            */

        case R.id.upload:
            Toast.makeText(this,"context " +getString(R.string.upload),Toast.LENGTH_SHORT).show();
            break;
        case R.id.download:
            Toast.makeText(this,"context " +getString(R.string.download),Toast.LENGTH_SHORT).show();
            break;
        case R.id.stop:
            Toast.makeText(this,"context " +getString(R.string.stop),Toast.LENGTH_SHORT).show();
            break;
        case R.id.quit:
            Toast.makeText(this,"context " +getString(R.string.quit),Toast.LENGTH_SHORT).show();
            break;


    }
    return super.onContextItemSelected(item);
}

// 系统三个点 菜单


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /*
    menu.add(0,1,1,"上传");
    menu.add(0,2,2,"下载");
    */


    getMenuInflater().inflate(R.menu.main_menu,menu);

    return  super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
    /*    case 1:
            Toast.makeText(this,"options upload",Toast.LENGTH_SHORT).show();
            break;
        case 2:
            Toast.makeText(this, "options downloud", Toast.LENGTH_SHORT).show();
            break;
            */

        case R.id.upload:
            Toast.makeText(this,"options " +getString(R.string.upload),Toast.LENGTH_SHORT).show();
            break;
        case R.id.download:
            Toast.makeText(this,"options " +getString(R.string.download),Toast.LENGTH_SHORT).show();
            break;
        case R.id.stop:
            Toast.makeText(this,"options " +getString(R.string.stop),Toast.LENGTH_SHORT).show();
            break;
        case R.id.quit:
            Toast.makeText(this,"options " +getString(R.string.quit),Toast.LENGTH_SHORT).show();
            break;


    }
    return super.onOptionsItemSelected(item);
}

}

上一篇下一篇

猜你喜欢

热点阅读