我在滑动标签中使用了几个片段。我创建的最后一个在我的应用程序中正确显示,但点击按钮和编辑文本并没有做任何事情。我认为这是我在工作室中创建片段的方式。
我该如何调试这种情况?
我添加了java代码:
public class StoreWords extends Fragment {
public StoreWords() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.store_words_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initFileSave();
add_w_edit = (EditText) getActivity().findViewById(R.id.add_diffwords_edit);
Button add = (Button) getActivity().findViewById(R.id.add_diffwords);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addWords();
}
});
Button del = (Button) getActivity().findViewById(R.id.remove_diffwords);
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addWords();
}
});
}
和布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bernard.speechparole.StoreWords"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/text_words"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_diffwords"
android:layout_marginLeft="10dp"
android:text="@string/add"
android:layout_marginTop="15dp"
android:layout_below="@id/text_words"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/add_diffwords_edit"
android:layout_toRightOf="@id/add_diffwords"
android:layout_marginLeft="30dp"
android:layout_alignBottom="@id/add_diffwords"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/remove_diffwords"
android:text="@string/delete"
android:layout_below="@+id/add_diffwords"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:fillViewport="false">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="@+id/list_words"
android:layout_marginTop="10dp"
android:layout_below="@id/remove_diffwords"
/>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:0)
执行onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.store_words_fragment,
container, false);
Button button = (Button) view.findViewById(R.id.add_diffwords):
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// your code here
}
});
return view;
}
答案 1 :(得分:0)
public class StoreWords extends Fragment {
public StoreWords() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview=inflater.inflate(R.layout.store_words_fragment, container, false)
add_w_edit = (EditText) rootview.findViewById(R.id.add_diffwords_edit);
Button add = (Button) rootview.findViewById(R.id.add_diffwords);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addWords();
}
});
Button del = (Button) getActivity().findViewById(R.id.remove_diffwords);
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addWords();
}
});
return rootview;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initFileSave();
}
答案 2 :(得分:0)
您要扩展片段布局的问题,但是当您将侦听器连接到视图时,您会在活动布局中搜索它们。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.store_words_fragment, null, false);
Button button = (Button) view.findViewById(R.id.add_diffwords):
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
addWords();
}
});
//Other code and views that you want handle events
return view;
}
查看本文如何使用片段。 http://developer.android.com/guide/components/fragments.html
当你打电话给这个 。getActivity()findViewById(R.id.add_diffwords);这意味着您尝试搜索并设置侦听器以在活动布局中查看。