gpt4 book ai didi

android - onClickListener 在 fragment 中不起作用

转载 作者:太空狗 更新时间:2023-10-29 16:41:22 25 4
gpt4 key购买 nike

public class MainFragment extends Fragment {
public static final String TAG = MainFragment.class.getSimpleName();
private static final String ABOUT_SCHEME = "settings";
private static final String ABOUT_AUTHORITY = "main";
public static final Uri ABOUT_URI = new Uri.Builder().scheme(ABOUT_SCHEME).authority(ABOUT_AUTHORITY).build();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.mainbutton, container, false);

return v;
}

}

根据以下链接: How to handle button clicks using the XML onClick within Fragments

public class StartFragment extends Fragment implements OnClickListener{


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_start, container, false);

Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(this);
return v;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.StartButton:

/* ... */

break;
}
}
}

源也不起作用。

最佳答案

@J.Romero 是对的。试试这段代码,我更改了 onClick 方法并添加了一些调试日志。

public class StartFragment extends Fragment implements OnClickListener {

private static final LOG_TAG = "com.example.activity"

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_start, container, false);

if (v != null) {
Button mButton = (Button) v.findViewById(R.id.StartButton);
Log.d(TAG, "View is not null");

if (mButton != null) {
b.setOnClickListener(this);
Log.d(TAG, "mButton is not null");
}
}

return v;
}

@Override
public void onClick(View v) {
if (v == mButton) {
//do something
}
}
}

关于android - onClickListener 在 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220701/

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