[英]Button OnClickListener giving ViewPostImeInputStage ACTION_DOWN error
[英]Android Toolbar Button Error - ViewPostImeInputStage ACTION_DOWN
我想在工具欄中創建一個按鈕,而不是和圖標,到此為止。 我知道我需要創建一個OnClickListener,但是無法實現。 我嘗試的一切都會導致錯誤
ViewPostImeInputStage ACTION_DOWN
這是我的menu_home_activity.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.getmore.getmoreplus.activity.HomeActivity">
<item
android:id="@+id/action_call"
app:actionLayout="@layout/callicon"
android:title=" "
app:showAsAction="ifRoom"
/>
</menu>
和我的callicon.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:paddingTop="10dp">
<ImageView
android:id="@+id/callbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/call" />
</RelativeLayout>
最后是我的HomeFragment.java文件
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Creates options in the top bar
//Currently only creates the phone
inflater.inflate(R.menu.menu_home_activity, menu);
super.onCreateOptionsMenu(menu, inflater);
}
任何幫助將不勝感激。 對不起,仍然是編程的新手。
您不需要為工具欄圖標提供onclick列表器,可以通過以下方式實現相同的功能
onOptionsItemSelected()
首先,您可以直接在菜單xml中代替app:actionLayout="@layout/callicon"
來直接提供圖標
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.getmore.getmoreplus.activity.HomeActivity">
<item
android:id="@+id/action_call"
android:title="Call"
app:showAsAction="ifRoom"
android:icon="@drawable/call"/>
在您的活動中
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_activity, menu);
return super.onCreateOptionsMenu(menu);
}
然后這個
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_call) {
// Do your onClick action here
}
return super.onOptionsItemSelected(item);
}
我認為可以解決您的問題
好吧,我修好了。
我將此添加到了我的活動課
private ImageView callbutton;
然后,我在int()部分添加了一個偵聽器。 工作
callbutton = (ImageView)mRootView.findViewById(R.id.callbtn);
callbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//my action
}
});
如此簡單,但對於像這樣的新手來說,則需要花費更長的時間才能解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.