Android OnItemClick Switch Case Toast消息(Android OnItemClick Switch Case Toast Message)
我试图这样做,当用户点击图像时,会弹出一个带有相关名称的Toast。 我想我需要用switch语句来做这个,因为我有一个数组,但我不确定这是否真的是正确的方法。
这是我的代码:
public class MainActivity extends AppCompatActivity { Integer[] Family = {R.drawable.angelica, R.drawable.dad, R.drawable.enzoandbully, R.drawable.enzokathyalex, R.drawable.ernesto, R.drawable.gale, R.drawable.joel, R.drawable.lorenzo}; ImageView pic; Integer member; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView grid = (GridView) findViewById(R.id.gridView); final ImageView pic = (ImageView) findViewById(R.id.imgFamily); grid.setAdapter(new ImageAdapter(this)); grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getBaseContext(), " " + (position + 1), Toast.LENGTH_SHORT).show( ); pic.setImageResource(Family[position]); } }); } public class ImageAdapter extends BaseAdapter { private Context context; public ImageAdapter(Context c) { context = c; } @Override public int getCount() { return Family.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { pic = new ImageView(context); pic.setImageResource(Family[position]); pic.setScaleType(ImageView.ScaleType.FIT_XY); pic.setLayoutParams(new GridView.LayoutParams(330,300)); return pic; } } }
编辑:忘记XML代码:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:paddingBottom="16dp" tools:context=".MainActivity"> <GridView android:layout_width="wrap_content" android:layout_height="300dp" android:id="@+id/gridView" android:numColumns="2" android:columnWidth="160dp" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:gravity="center" /> <ImageView android:id="@+id/imgFamily" android:layout_width="128dp" android:layout_height="128dp" android:layout_gravity="center_horizontal" android:contentDescription="@string/imgFamily" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_row="12" android:layout_column="0" /> </GridLayout>
I'm trying to make it so when the user taps an image, a Toast pops up with the relevant name. I think I need to do this with a switch statement since I have an array, but am unsure if that's actually the correct way.
Here's my code:
public class MainActivity extends AppCompatActivity { Integer[] Family = {R.drawable.angelica, R.drawable.dad, R.drawable.enzoandbully, R.drawable.enzokathyalex, R.drawable.ernesto, R.drawable.gale, R.drawable.joel, R.drawable.lorenzo}; ImageView pic; Integer member; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView grid = (GridView) findViewById(R.id.gridView); final ImageView pic = (ImageView) findViewById(R.id.imgFamily); grid.setAdapter(new ImageAdapter(this)); grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getBaseContext(), " " + (position + 1), Toast.LENGTH_SHORT).show( ); pic.setImageResource(Family[position]); } }); } public class ImageAdapter extends BaseAdapter { private Context context; public ImageAdapter(Context c) { context = c; } @Override public int getCount() { return Family.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { pic = new ImageView(context); pic.setImageResource(Family[position]); pic.setScaleType(ImageView.ScaleType.FIT_XY); pic.setLayoutParams(new GridView.LayoutParams(330,300)); return pic; } } }
Edit: Forgot XML Code:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:paddingBottom="16dp" tools:context=".MainActivity"> <GridView android:layout_width="wrap_content" android:layout_height="300dp" android:id="@+id/gridView" android:numColumns="2" android:columnWidth="160dp" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:gravity="center" /> <ImageView android:id="@+id/imgFamily" android:layout_width="128dp" android:layout_height="128dp" android:layout_gravity="center_horizontal" android:contentDescription="@string/imgFamily" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_row="12" android:layout_column="0" /> </GridLayout>
原文:https://stackoverflow.com/questions/33622824
更新时间:2023-04-12 15:04
最满意答案
所以你有几行看起来像这样:
self.set_html("<h2> {0} </h2>").format(self.logout_link.get_html())
您可能希望它们看起来像:
self.set_html("<h2> {0} </h2>".format(self.logout_link.get_html()))
So you have a few lines that looks like this:
self.set_html("<h2> {0} </h2>").format(self.logout_link.get_html())
You probably want them to look like:
self.set_html("<h2> {0} </h2>".format(self.logout_link.get_html()))
相关问答
更多-
我们可以在MVC设计模式中使用Decorator设计模式吗?(Can we use Decorator design pattern inside MVC design pattern?)[2023-02-03]
是的,“一般来说”你可以很容易地拥有一个支持decorator模式的MVC框架。 特别是对于春天,是的,你可以“做到”(我个人不知道如何)但是这里有一个关于它的SO问题,解释如何解决别人对它的实现。 (所以'可以'完成) sitemesh和spring MVC装饰模式问题 Yes, "In general" you could easily have a MVC framework that supports the decorator pattern. For spring in particular, ... -
装饰设计模式问题?(Decorator design pattern issue?)[2023-09-20]
例如,他们谈论的是检查饮料2类型的代码 if (beverage2 instanceof DarkRoast) {} 一旦用Mocha或Whip装饰drink2,它就不再是DarkRoast了。 编辑:我还应该提一下,通常使用instanceof表示一个不完整使用OO的糟糕设计,这表示它在某些情况下很有用。 They're talking about code that checks the type of beverage2, for example if (be ... -
我不知道我是否正确地得到了这个问题,但是如果你想要一个方法drawBlackAndWhite() (假设它是RedShapeDecorator.draw()的模拟),你可以在你的装饰器中定义其他方法。 要在装饰器中获取draw3D()方法,您应该创建一个新的Shape接口实现,它可以绘制3D对象(因为现有实现Circle和Rectangle是平的)。 如果你希望所有这些方法在你的装饰器的一个实例中放入多个Shape字段。 但是由于Shape接口只有一个方法draw()所以你将无法通过引用调用方法drawBl ...
-
python @decorator与装饰器设计模式有关吗?(Is python @decorator related to the decorator design pattern?)[2023-07-08]
不,这不是一个奇怪的巧合,不,python @decorators不实现GOF装饰器模式。 GOF(四人帮,以“设计模式”这本书的四位作者命名)装饰模式增加了“装饰”来改变对象的行为。 它通过创建可应用于未装饰基类的“装饰器”类来实现此目的。 Python装饰器为功能或类添加了“装饰”。 这些装饰器本质上是包装或修改函数/类的函数。 两者都是类似的,你可以重复使用装饰器多次添加相同的功能,但装饰器模式要求装饰器与你可以装饰的类一起设计。 Python装饰器反而只是在任何类的函数上进行工作。 No, it's ... -
了解作曲家和作曲家之间的区别很重要。 装饰器是一种组合形式,但主要的一点是,它可以让包装器被通常使用装饰对象的代码使用。 让我们用一个常见的例子来帮助探索这个问题。 考虑接口InputStream 。 我可能有一个方法将字节从一个流复制到另一个流: public static void copy(InputStream in, OutputStream out) { ... } 现在说我们有一个我们想要复制的文件。 我会创建一个FileInputStream并将其传递给copy() 。 但是说我得到一个要 ...
-
设计模式的帮助(Design pattern help)[2022-09-18]
我可能会遇到这样一个问题的设计模式是: 依赖注入和一个框架,Composite模式和Builder模式。 他们应该基本上让你将机器人的创作与他们的使用分开。 The design patterns I'd probably go with for such a problem are: Dependency Injection and a framework to go with it, the Composite pattern, and the Builder pattern. They should ... -
所以你有几行看起来像这样: self.set_html("
{0}
").format(self.logout_link.get_html()) 您可能希望它们看起来像: self.set_html("{0}
".format(self.logout_link.get_html())) So you have a few lines that looks like this: self.set_html("{0}
").f ... -
装饰者是一种通用模式; 它可能意味着多种东西,具体取决于域名。 它们也被称为“包装器”或“适配器”,IMO更适用于功能包装范例。 也就是说,不要过于依赖精确的措辞:它们是模式 ,而不是一成不变的法则。 Decorators are a generic pattern; it can mean multiple things depending on the domain. They're also called "wrappers" or "adapters", which IMO is more appl ...
-
装饰模式中的困惑(Confusion in Decorator Pattern)[2024-02-02]
假设Email是Java界面。 任何实现Email类都可以用来初始化变量email包括任何装饰器,因为装饰器实现接口或扩展抽象类(实现接口)。 当然,客户端可以知道什么是类实例,但他们通常不需要这样做。 只需使用email.getClass() 。 另一方面,装饰器与装饰物不是同一个物体。 它包装装饰物体。 因此TextEmail和SecuredEmail不是同一个对象。 如果代码看起来像: TextEmail txtEmail = new TextEmail(); SecuredEmail secured ... -
你应该阅读这个解释什么是python的装饰器。 我们与Python有关的“装饰者”与DecoratorPattern [...]并不完全相同。 Python装饰器是对Python语法的一种特定更改,它允许我们更方便地更改函数和方法(以及未来版本中可能的类)。 这支持DecoratorPattern的更易读的应用程序,但也支持其他用途。 因此,您将能够使用python的装饰器实现“经典”装饰器模式,但您将能够使用这些装饰器做更多(有趣;))事情。 在您的情况下,它可能看起来像: def applyToppin ...