0

我有一个我制作的音调列表,我已经设法将它们放入一个列表中,但现在我面临着允许用户“触摸并按住”以指定为通知音的挑战。这是 MainActivity.java 中的代码:

       import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
s = new Sound();
s.setDescription("Axo");
s.setSoundResourceId(R.raw.axo);
mSounds.add(s);
s = new Sound();
s.setDescription("Basix");
s.setSoundResourceId(R.raw.basix);
mSounds.add(s);
s = new Sound();
s.setDescription("Bender");
s.setSoundResourceId(R.raw.bender);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();

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

我还有另外两个 java 文件,一个是“声音类”,另一个是“声音适配器”,尽管我认为它们与我的问题无关。

只是让你们知道,我花了几周的时间才走到这一步,因为我还是 javaland 的大一新生。这里的任何帮助或示例都会很热门!地狱,如果你想为我做这项工作,那就更甜蜜了!

4

1 回答 1

0

我发布的解决方案只是让您能够检测您单击的列表项的位置并将其设置为铃声(这不起作用),在setRingtone函数内部发生了魔法。所有将声音作为铃声的代码都必须放在那里

在下面的switch更改case id 'main_menu'item idmenu -> context_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/main_menu"
    android:title="@string/set_ringtone"
    android:showAsAction="ifRoom"
    />
</menu>

onCreateContextMenu在下面的代码下方添加。

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

 /*-----------------------------  add code below ------------------------------*/

 @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info;
        try {
            info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        } catch (ClassCastException e) {
            Log.e("", "bad menuInfo", e);
            return false;
        }

        switch (item.getItemId()){
            case R.id.main_menu:
                setRingtone(item.getItemId(), info.position);
                //Toast.makeText(this, "id = " + item.getItemId() + " pos =     " + info.position, Toast.LENGTH_SHORT).show();
                break;
            default:
                return false;
        }

        return true;
    }

    public  void setRingtone(int id, int pos){
        //RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);

        Toast.makeText(this, "id = " + id + " pos = " + pos, Toast.LENGTH_SHORT).show();
    }
于 2015-02-15T20:38:04.880 回答