我真的無法在互聯網上找到這個問題的答案。我正在嘗試通過Android音量按鈕瀏覽XML項目。 StackOverflow上給了我下面的代碼,這樣做的:無法解析符號KeyEvent
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
但是,當我嘗試這段代碼粘貼在我的MainActivity.java,「KeyEvent的」變成紅色與錯誤「無法解析符號KeyEvent的」。這是來自MainActivity.java的代碼
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.InputEvent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
ListView lvPcsPost;
ArrayList<PostValue> postValueArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvPcsPost = (ListView) findViewById(R.id.lvPcsPost);
lvPcsPost.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (postValueArrayList != null && postValueArrayList.size() > 0) {
Intent intentShowPost = new Intent(Intent.ACTION_VIEW, Uri.parse(postValueArrayList.get(position).getVid()));
startActivity(intentShowPost);
}
}
});
new PostAsync().execute();
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
class PostAsync extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
XMLHelper helper;
@Override
protected void onPreExecute() {
pd = ProgressDialog.show(MainActivity.this, "Please wait...", "Loading", true, false);
}
@Override
protected Void doInBackground(Void... arg0) {
helper = new XMLHelper();
helper.get();
postValueArrayList = helper.getPostsList();
return null;
}
@Override
protected void onPostExecute(Void result) {
PostBaseAdapter postBaseAdapter = new PostBaseAdapter(MainActivity.this, postValueArrayList);
lvPcsPost.setAdapter(postBaseAdapter);
pd.dismiss();
}
}
}
任何在這裏看到問題的人?
進口'KeyEvent'? – csmckelvey 2015-02-07 14:44:35