我可能会遗漏一些简单的东西吗?"kruis"图片显示在我的ImageButton上,所以我非常确定我的main.xml是好的,但是当我点击ImageButton时,我没有Toast,而testView也没有改变......一直在挣扎几个小时现在,不知道我做错了什么!
package com.matchit;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class matchit extends Activity {
OnClickListener cardListener;
TextView testView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testView = (TextView)findViewById(R.id.test);
ImageButton b1 = (ImageButton)findViewById(R.id.card1);
b1.setImageResource(R.drawable.kruis);
b1.setOnClickListener(cardListener);
cardListener = new OnClickListener(){
@Override
public void onClick(View v) {
testView.setText("its working");
Toast.makeText(getApplicationContext(),
"its working",
Toast.LENGTH_LONG).show();
}
};
}
Run Code Online (Sandbox Code Playgroud)
}
我有一个带有图像按钮的应用程序,它既有onclick又有onlongclick监听器.但是,当长按按钮时,这两个侦听器都在执行.有什么建议?
d1.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
selectMode = true;
dockNum = 1;
sd1.open();
d1.cancelLongPress();
return false;
}
});
...d1.setOnClickListener(this);
...case R.id.d1:
if(d1s.equals("empty")) {
selectMode = true;
dockNum = 1;
sd1.open();
} else {
Intent d1i = pm.getLaunchIntentForPackage(d1s);
startActivity(d1i);
}
break;
Run Code Online (Sandbox Code Playgroud) 我想创建一个ImageButton并将图像设置为保持原始大小的背景.所以我在StackOverflow上也使用了几行代码:
ImageButton retry = new ImageButton(this);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.button2);
retry.setImageBitmap(image);
retry.setMinimumWidth(image.getWidth());
retry.setMinimumHeight(image.getHeight());
Run Code Online (Sandbox Code Playgroud)
但不幸的是我获得了以下结果:
显然我不想要"背景按钮",而只需要图像.我能怎么做?
我的应用程序中有一个ArrayIndexOutofBounds异常.我想在我的应用程序中放置一个前一个按钮.我的数组中有5个元素按钮循环.如何在用户点击元素[0]时这样做,它将循环回元素[4]而不是转到[-1]导致异常?
我的代码如下:
mPrevButton = (ImageButton)findViewById(R.id.prev_button);
mPrevButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
}
}
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么:
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
Run Code Online (Sandbox Code Playgroud) 我有一个listview,我添加了1个图像按钮的行..我试图设置图像按钮setfocusable false但仍然无法正常工作..
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:descendantFocusability="blocksDescendants"
android:id="@+id/RL_item">
<ImageButton
android:layout_width="200dp"
android:layout_height="fill_parent"
android:id="@+id/imageButton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/LL_Installed"
android:layout_alignRight="@+id/textView2"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/item_list_left_right"
android:paddingLeft="@dimen/item_list_left_right"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/imageView"
android:background="@drawable/tick"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/list_item_installed"
android:id="@+id/textView3"
android:paddingLeft="6dp"
android:textColor="#ffffff"
style="@style/TextShadow"
android:gravity="center_vertical|fill_vertical"
android:layout_weight="1"
android:layout_width="0dip"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
style="@style/TextShadow"
android:paddingTop="@dimen/item_list_top"
android:paddingRight="@dimen/item_list_left_right"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" …
Run Code Online (Sandbox Code Playgroud) 我添加的图片48*48
,72*72
并96*96
到mdpi
,ldpi
和hdpi
各自的.
并添加以下代码 AndroidManifest.xml
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
Run Code Online (Sandbox Code Playgroud)
第一个问题:
当我执行上述操作时,应用程序将自动捕获合适的图片?
第二个问题:
但是如何在xml
文件中设置Button ?
如果应用程序本身将捕捉到合适的图片,所以我已经设置了width
和height
以match_parent
类似下面的代码?
<LinearLayout
android:id="@+id/ImageBtulayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:background="#000000"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/BackButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:background="@drawable/back"/>
<ImageButton
android:id="@+id/recordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:background="@drawable/no_delete" />
<ImageButton
android:id="@+id/download_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:background="@drawable/download"/>
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我像上面那样修改代码.
我将APP安装到尺寸为4.7英寸的设备上.
但图标似乎太小了.
如下图所示
如何删除imageButton上的灰色部分? 有没有错?
我有一些动态声明ImageButtons
,这些ImageButtons
没有'并且它们在a中声明LinearLayout
,我想创建一个方法来ImageButtons
在调用时更改这些的图像资源,因为我没有为这些ImageButtons
我预定义的ID '中号使用功能getChildAt()
与LinearLayout
,但getChildAt()
不提供setImageResource()
的,它只是提供setBackgroundResource()
这个功能不进行替换的旧形象,也并不适合在旧图片,因为它提供了一个背景不是图像资源,是什么我能做到吗?
这是我的方法代码:
private void my_method(int drawable) {
int count = Righter.getChildCount(); // Righter is the LinearLayout
for (int i = 1; i < count; i++) {
Righter.getChildAt(i).setBackgroundResource(drawable); // here is where i change the background image
Righter.getChildAt(i).setClickable(true);
}
}
Run Code Online (Sandbox Code Playgroud) 我在使用Python(3.5)中的ttk / tkinter搞砸了,而按钮也遇到了一些问题(特别是ttk.Button)。
无论我尝试什么,都无法终生获得图像显示在按钮上。
我有以下代码:
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
class Example(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
master.title('Button Test')
self.configure(padding='10 10 10 10')
self.grid(column=0, row=0, sticky=(N, E, W, S))
buttonImage = Image.open('play.png')
buttonPhoto = ImageTk.PhotoImage(buttonImage)
myButton = ttk.Button(self, image=buttonPhoto, padding='10 10 10 10')
myButton.grid(column=1, row=1, sticky=(E, W))
if __name__ == "__main__":
root = Tk()
example = Example(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
这是我在此处,此处以及在StackOverflow上的用户中找到的示例的组合。
预先感谢您的任何想法/见解。
-西恩
imagebutton ×8
android ×7
eclipse ×1
image ×1
java ×1
onclick ×1
python ×1
python-3.5 ×1
tkinter ×1
ttk ×1