我有以下xmlFile:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.SPTechnos.ambassador.Home"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="@+id/LinearBG"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/ToolBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#263359"
android:gravity="top" >
<ImageButton
android:id="@+id/imageButton_Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@android:drawable/ic_menu_revert"
android:visibility="invisible" />
<ImageButton
android:id="@+id/imageButton_Preferences"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@android:drawable/ic_menu_preferences" />
<ImageButton
android:id="@+id/imageButton_Map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@android:drawable/ic_menu_mapmode" />
</RelativeLayout>
<LinearLayout
android:id="@+id/Content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill"
android:orientation="vertical" >
<EditText
android:id="@+id/editText_1"
android:layout_width="match_parent"
android:layout_height="189dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</LinearLayout>
<ImageButton
android:id="@+id/imageButton_Speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/ic_btn_speak_now" />
<EditText
android:id="@+id/editText_2"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_weight="0.79"
android:ems="10"
android:inputType="textMultiLine" />
</LinearLayout>
</FrameLayout>
在oncreate
中与以下代码匹配ImageButton speak = (ImageButton) findViewById(R.id.imageButton_Speak);
speak.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
/*Toast.makeText(Home.this, "test", Toast.LENGTH_SHORT).show();
if(!mIsListening)
{
mSpeechRecognizer.startListening(mSpeechRecognizerIntent); // Listen
Log.d("OUTPUT", "Button Pressed");
}*/
Log.d("OUTPUT", "Button Pressed");
}
});
然而,当我点击按钮没有打印到logcat时,我已尝试使用Toasts进行测试,并且这两个证明了onclick没有触发,我搜索了过去一小时但找不到原因。对我来说,我必须忽略一些愚蠢的东西......
答案 0 :(得分:1)
我尝试了这段代码并且有效。如果设备已连接,请检查您的logCat。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton speak = (ImageButton) findViewById(R.id.imageButton_Speak);
speak.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(MainActivity.this, "teste", Toast.LENGTH_SHORT).show();
}
});
}
}
答案 1 :(得分:0)
尝试在您想要在XML中单击的imageView上设置可点击
android:clickable="true"
或尝试使用代码
speak.setClickable(boolean)