gpt4 book ai didi

java - 从 OnItemClick 扩充菜单

转载 作者:行者123 更新时间:2023-12-01 14:54:37 27 4
gpt4 key购买 nike

我正在尝试从 OnItemClick 中扩充菜单。我查看了 Android 开发者网站,并能够使用下面的第一个代码来充气 toast 。然而,当我尝试用菜单充气机替换它时,它不起作用。你能帮我解决这个问题吗?第一组代码是可以工作的 toast 充气器,第二组代码是不能工作的菜单充气器。当我点击某个项目时, toast 会弹出,但菜单不会。该程序将无法运行,并说我在菜单充气器中存在多个语法错误。虽然我无法解决它们。

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

}



}

这可以正常工作。然而,当我尝试膨胀菜单而不是 toast 时,却没有。这是不起作用的代码,并且菜单膨胀器中存在语法错误。无论我尝试做什么,我都无法更正代码。

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

}



}

最佳答案

您应该尝试将 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);
}

请注意,一旦用户点击某个项目,就会弹出上下文菜单。

关于java - 从 OnItemClick 扩充菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14429719/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com