我已经设置了一个位于片段中的EditText的onClick属性:
<EditText android:id="@+id/edittext1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:inputType="none" android:maxLines="1"
android:singleLine="true" android:layout_marginTop="16dp"
android:focusable="false"
android:longClickable="false"
android:clickable="true"
android:onClick="doSomething"
android:cursorVisible="false"
android:editable="false">
然后在片段类中我有:
public void doSomething(View view) {
//show dialogfragment...
}
但方法doSomething
显示为灰色,我收到警告'方法doSomething永远不会被使用'。
注意:此代码最初是在一项活动中运行正常。
还有另一种处理onClick片段的方法吗?
答案 0 :(得分:1)
首先在$> bash test.sh 3 2 1 45 45 3 4 4 4 1 1 1 1
1
2
3
4
45
EditText
个实例
fragment
然后
在EditText et;
添加:
onCreateView()
然后实施您的 etEmployee=(EditText)rootView.findViewById(R.id.edittext1);
onClickListener()
注意:在片段中,您必须引用 et.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//what ever you need to do goes here
}
});
才能在这种情况下访问inflatedView
rootView。
您正在使用的代码也不起作用因为您正在使用片段并且在xml中使用editText
属性只会使您能够在包含onClick
的{{1}}中使用它1}}。
答案 1 :(得分:0)
如果对函数调用方式无关紧要,可以尝试将OnClickListener添加到EditText。
@person.dog
答案 2 :(得分:0)
如果您在片段中添加android:onClick="doSomething"
,则会在您的活动中调用方法,但不会在Activity
中调用。如果您希望片段中的回调通过实际操作添加。
edittext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Your code.
}
});
答案 3 :(得分:0)
在布局XML中尝试指定tools:context="com.mypackage.path.MyFragment"
。不知道它是否会为'onClick'修复它,但它在过去为我修复了类似的问题。
这是布局文件中最顶级的“ViewGroup”,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mypackage.path.MyFragment" />