我有这个布局,但按钮不可点击。
我尝试过以前帖子的解决方案,例如将android:focusable="false"
添加到按钮或列表视图或线性布局,但它没有用。我尝试在按钮button.setFocusable(false)
之后添加:OnClickListener
,但也没有用。我甚至尝试将android:descendantFocusability="blocksDescendants"
添加到主线性布局,但问题仍然存在。
谢谢。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
<EditText
android:id="@+id/search_field"
android:layout_width="181dp"
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:ems="10"
android:hint="Search"
android:textColor="#1F6260" />
<Button
android:id="@+id/refresh_button"
android:layout_width="34dp"
android:layout_height="32dp"
android:background="@drawable/refresh3" />
</LinearLayout>
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector">
</ListView>
</LinearLayout>
Java代码:
refresh= (Button)myFragmentView.findViewById(R.id.refresh_button);
refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((MainTabs)getActivity()).refresh();
((MainTabs)getActivity()).getUsersList();
}
});
在onclick方法中添加了toast并显示,因此我的代码无效,按钮没有任何问题。很抱歉给您带来不便。 感谢。
答案 0 :(得分:1)
实现此目的的两种方法
1
像这样更改你的按钮
<Button
android:id="@+id/refresh_button"
android:layout_width="34dp"
android:layout_height="32dp"
android:background="@drawable/refresh3"
android:clickable="true"
android:onClick="buttonClick" />
然后在您的活动中创建一个方法,该方法必须像这样传递您的View作为参数。
public void buttonClick(View v) {}
或者你可以按照第二种方式
2
Button button;
button = (Button) findViewById(R.id.refresh_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
希望这会对你有所帮助。