我正在尝试从OnItemClick中提取菜单。我看了android开发者网站,并且能够使用下面的第一个代码给吐司充气。但是,当我尝试用Menu Inflater替换它时,它不起作用。你能帮我解决这个问题吗?第一组代码是有效的toast inflater,第二组代码是没有的Menu Inflater。当我点击某个项目时,吐司弹出,但菜单不会。该程序将无法运行,并说我在菜单inflater中有多个语法错误。虽然我无法解决它们。
package com.carouseldemo.main;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselItem;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.main.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Welcome to Power Source International");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
这可以正常工作。但是,当我尝试膨胀菜单而不是吐司时,它却没有。这是不起作用的代码,并且在菜单inflater中有语法错误。无论我尝试做什么,我都无法纠正代码。
package com.carouseldemo.main;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselItem;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.main.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
private Object v;
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
@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);
}
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
对此有何建议?先感谢您。
更新代码:
package com.carouseldemo.main;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselItem;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.main.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
openContextMenu(view);
}
@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);
// TODO Auto-generated method stub
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
更新2:
package com.carouseldemo.main;
import android.app.Activity;
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.view.View.OnClickListener;
import android.widget.TextView;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
public class MainActivity extends Activity implements OnClickListener {
@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);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
openContextMenu(view);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
您应该尝试将onCreateContextMenu()
方法移动到Activity的根目录,这样看起来像这样:
public class MainActivity extends Activity implements OnClickListener {
@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);
}
@Override
public void onCreate(Bundle savedInstanceState) {
//implementation
}
//rest of code
}
然后你可能会想要这样的东西:
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
openContextMenu(view);
}
请注意,只要用户点按某个项目,就会弹出“上下文”菜单。