多个按钮OnClickListener没有做任何事情。(Multiple button OnClickListener not doing anything. (Using a switch))
我正在尝试创建一个应用主页,这将引导我的其他一些应用创意。 主页由6个按钮组成,我希望它们在点击时可以指向各自的页面。 为了检查这些按钮是否有效,我使用过多士(一旦祝酒工作,我会将它们改为意图)。
这是我的代码,为了简化和缩短代码,我只在一个按钮后显示代码,但相同的代码适用于我的android工作室中的其他5个代码。
首先,XML:
<Button android:id="@+id/spotify_button" android:text="Spotify Streamer" android:textAllCaps="true" android:layout_width="match_parent" android:layout_height="60dp" />
在MainActivity中:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button spotify = (Button)findViewById(R.id.spotify_button); spotify.setOnClickListener(buttonz); } private View.OnClickListener buttonz = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.spotify_button: Toast.makeText(getApplicationContext(), "Text", Toast.LENGTH_SHORT); break;
作为Java和Android的新手,这就是我理解代码的工作方式:
在onCreate部分,我创建了一个名为“spotify”的按钮,该按钮被强制转换为XML按钮。 然后我在这个按钮上设置一个名为“buttonz”的onClickListener。 一旦“计算机”看到它,它就会查找我制作的部分(View.OnClickListener),它看到它在点击时起作用,所以它认为“点击按钮时会做一些事情我们会点击这里点击但点击了哪个按钮? ?它不知道,那就是切换到位的时候。一切似乎都联系起来所以我不知道为什么当我在手机上打开应用程序并点击按钮时没有任何反应?
无论如何,如果有人能帮助我完成这项工作,我将非常感激。 提前致谢。
I'm trying to make an app homepage that will lead on to some other of my app ideas. The homepage consists of 6 Buttons, and I want them to lead to their own individual pages when clicked on. To check if the buttons work, I have used toasts (Once the toasts work i'll change them into intents).
Here is my code, to simplify and shorten the code I've only shown the code following one button, but the same code applies for 5 others in my android studio.
First of all, XML:
<Button android:id="@+id/spotify_button" android:text="Spotify Streamer" android:textAllCaps="true" android:layout_width="match_parent" android:layout_height="60dp" />
And in the MainActivity:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button spotify = (Button)findViewById(R.id.spotify_button); spotify.setOnClickListener(buttonz); } private View.OnClickListener buttonz = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.spotify_button: Toast.makeText(getApplicationContext(), "Text", Toast.LENGTH_SHORT); break;
As someone very new to Java and Android, this is how I understand the code works:
In the onCreate section, I create a button called "spotify" which is cast to the XML button. I then set an onClickListener called "buttonz" on this button. Once the "computer" sees this it looks for the section I made (View.OnClickListener), which sees that it works on click, so it thinks "do something when the button is clicked we will handle click here but which button clicked??? It doesn't know, and that's when the switch comes into place. Everything seems to link up so I don't know why when i open the app on my phone and click the button nothing happens?
Anyways, if someone could help me make this work I would be very grateful. Thanks in advance.
原文:https://stackoverflow.com/questions/34700458
最满意答案
原因在于'rz'命令没有提交完整的转移,但没有给出任何错误。
我解压缩的包是其他人上传的破坏包。 我下载了另一个,最后做对了。
建议:使用以下命令首先检查包的完整性。
>md5sum sth.tar.gz show checksum >du -h st.tar.gz show human-readable file size
并感谢Kevin Guan的建议和帮助。
The reason lays in 'rz' command which did not commit a intact transfer but gave no error.
The package I unzipped was a broken one uploaded by others. I downloaded another and finally got it right.
suggestion: use following command to check your package's integrity first.
>md5sum sth.tar.gz show checksum >du -h st.tar.gz show human-readable file size
And thanks Kevin Guan for the suggestions and help.
相关问答
更多-
令人惊讶的原因是x[i]看起来更加微妙。 我们来看看下面的cython函数: %%cython def cy_sum(x): cdef double res=0.0 cdef int i for i in range(len(x)): res+=x[i] return res 并衡量其表现: import numpy as np a=np.random.random((2000,)) %timeit cy_sum(a) >>>1000 loops, best o ...
-
它可能很难看,但我这样做是为了强制安装/升级: from pkg_resources import parse_version ## Install numpy if it is not found or too old try: import numpy if parse_version(numpy.__version__) < parse_version('1.10'): print("numpy {} was found but is too old. Upgradi ...
-
Cython基本构建仍然失败(Cython basic build still failing)[2023-07-27]
因此,用户@martineau指出,在我的python安装中看起来有些出错。 Python 2.7.9是使用msvc_ver = 1500或Visual Studio 2008构建的,所以我为什么使用旧版本的Python(2.7.6)使用后来的编译器并不清楚。 我尝试在我的另一台计算机上使用干净的2.7.9安装,并且能够成功编译,而无需修改cygwinccompiler.py 。 So, it looks like something went awry in my installation of pyth ... -
类的Cython列表(Cython list of class)[2023-10-18]
cython扩展类是 python对象,所以没有任何实际的转换正在进行,但是您的一般意义是正确的,它可能会为生成的代码添加间接/开销。 最简单的事情就是声明你正在访问的列表元素的类型,这会产生更有效的属性访问 - 下面将进一步说明一个完整的例子。 除此之外,您可以将python对象放在一起,并使用c-structs数组,c ++类的向量等。 %%cython cdef class Acl_cy: cdef public int s0, s1, s2 cdef public str st0, ... -
在这两个其他实现中,我玩过 使用cython编译器指令删除numpy通常必须进行的一些检查 使用类型化的内存视图,以便我可以指定内存布局(有时它们通常比旧的缓冲区接口更快) 展开循环,以便我们不使用numpy的切片机: 否则,你只是通过cython使用numpy,而numpy已经在相当快的c代码中实现了这个。 方法: import numpy as np cimport numpy as np cimport cython def func1(): cdef np.ndarray[np.doub ...
-
这似乎是LLVM与GCC的关系 - 请参阅编译器资源管理器中的示例,这比numba吐出的噪声要小。 我在程序集中有点迷失,但相当清楚GCC输出有一个循环( jge到.L6 )而clang输出没有。 另请参阅GCC bugtracker上的此问题 。 在我的机器上(Windows x64), numba并不比smart慢,仅约9 ns。 这个开销似乎是由于numba的类型调度机制 - 如果你通过选择特定的重载来消除它,那么numba版本比python版本更快 这是我的时间 In [73]: %timeit n ...
-
这是Cython版本0.21和旧代码的问题。 最简单的解决方案是将Cython降级到0.20,这是有效的。 要使用较新的Cython,请删除下载的pyjnius并让buildozer下载最新版本: rm ~/.buildozer/android/packages/pyjnius-master.zip 但是较新的Cython还需要更新的Kivy代码。 在你的buildozer.spec中,将需求中的kivy==master更改为kivy==master以获取最新的Kivy代码。 This is an iss ...
-
您将附加+=的bytes对象。 这真的很慢,因为它必须每次都复制整个现有的bytes对象。 不要那样做。 一个更好的选择是使用bytearray ,并且只在末尾的bytearray中构建一个bytes对象。 You're appending to a bytes object with +=. That's really slow, since it has to copy the whole existing bytes object every time. Don't do that. One bett ...
-
原因在于'rz'命令没有提交完整的转移,但没有给出任何错误。 我解压缩的包是其他人上传的破坏包。 我下载了另一个,最后做对了。 建议:使用以下命令首先检查包的完整性。 >md5sum sth.tar.gz show checksum >du -h st.tar.gz show human-readable file size 并感谢Kevin Guan的建议和帮助。 The reason lays in 'rz' command which ...
-
Cython for Appengine(Cython For Appengine)[2023-05-27]
不,这显然是不可能的。 GAE对你在沙盒中运行的内容有很强的限制,而且最明确的限制之一是你不能运行任意C代码 : Python运行时环境的所有代码都必须是纯Python,不包括任何C扩展或必须编译的其他代码。 No, this is very clearly not possible. GAE has very strong restrictions on what you can run inside its sandbox, and one of the most definitively spelle ...