gpt4 book ai didi

android - OnClickListener 不响应 fragment 中的点击

转载 作者:太空狗 更新时间:2023-10-29 13:34:33 25 4
gpt4 key购买 nike

我正在将一个旧程序转移到 Tabs and Fragments 实现中。用户输入一个 Id 进行搜索,然后点击提交。但是我的“提交按钮”什么也没做。

我尝试了几种替代编码变体,但得到了相同的结果 - 死气沉沉!

这是XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/setUpAthletesTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#CCFFFF" >

<TextView
android:id="@+id/instructionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/instructionText"
android:textColor="#FF0000"
android:textAppearance="?android:attr/textAppearanceMedium"
/>

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:stretchColumns="*" >

<EditText
android:id="@+id/editTextAthlete0"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:hint="@string/hintText"
android:inputType="number" />

<TextView
android:id="@+id/textAthleteName0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:hint="@string/athleteHintName0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>

这是我的标签/fragment

public class Tab3Fragment extends Fragment {

/* (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Begin Insertion */
LayoutInflater myInflater = inflater;
ViewGroup myContainer = container;
if(inflater == null) myInflater = inflater;
if(container == null) myContainer = container;
Log.d(APP_TAG,"At line 50 of Tab3Fragment myInflater = " + myInflater + " and myContainer = " + myContainer);
getAthleteInfoFromExistingDatabase(myInflater,myContainer);

LinearLayout tab3LinearLayout = (LinearLayout) inflater.inflate(R.layout.tab_frag3_layout, container, false);

View mySubmitButton = tab3LinearLayout.findViewById(R.id.submitButton);
((Button) mySubmitButton).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Toast.makeText(Tab3Fragment.this.getActivity().getBaseContext(),
"clicked on Submit Button", Toast.LENGTH_LONG).show();

Intent intent = new Intent();
intent.setClass(getActivity(), RetrieveAthleteData.class);
startActivity(intent);
}
});

return (LinearLayout)inflater.inflate(R.layout.tab_frag3_layout, container, false);
}

当我点击提交时,Toast 框和 Intent 都没有触发。

感谢您查看我的问题。

问题已解决!感谢 Dirk 建议将名称更改为全部小写。

最佳答案

在您的 xml 中没有 ID 为“submitButton”的按钮。

你应该只使用小写,因此给它 id "@+id/submit_button"

尽管我找不到不允许使用驼峰式大小写 ID 的来源。甚至 Google 有时也会在他们的示例中使用驼峰式 ID。

关于android - OnClickListener 不响应 fragment 中的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12013297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com