java-无法单击Android按钮

java-无法单击Android按钮,第1张

概述我正在创建一个应用程序,该应用程序在顶部使用带有一些按钮的片段之类的工具栏.按钮以编程方式添加.问题是,当我启动该应用程序时,会出现工具栏和按钮,但是无法单击按钮.当将鼠标悬停在按钮上(我使用模拟器)或单击按钮时,它们根本不会改变,也不会通知我的OnClickListener.但是,其

我正在创建一个应用程序,该应用程序在顶部使用带有一些按钮的片段之类的工具栏.按钮以编程方式添加.

问题是,当我启动该应用程序时,会出现工具栏和按钮,但是无法单击按钮.当将鼠标悬停在按钮上(我使用模拟器)或单击按钮时,它们根本不会改变,也不会通知我的OnClickListener.但是,其下面片段中的按钮和其他组件可以正常工作.

The Toolbar’s code:

public class ToolbarFragment extends Fragment implements VIEw.OnClickListener{public static final String LOG_KEY = "SM_TOolbar";public static final String TO_ADD_Feed_KEY = "TO_ADD_Feed_KEY";public static final String TO_ADD_PROfile_KEY = "TO_ADD_PROfile_KEY";public static final String TO_ADD_FRIENDS_KEY = "TO_ADD_FRIENDS_KEY";private button Feed;private button profile;private button frIEnds;private button logout;public ToolbarFragment() {    // required empty public constructor}private button addbutton(int stringID, linearLayout linearLayout) {    button button = new button(getContext());    button.setText(stringID);    button.setonClickListener(this);    linearLayout.addVIEw(button);    return button;}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,                         Bundle savedInstanceState) {    // Inflate the layout for this fragment    linearLayout linearLayout = (linearLayout)inflater.inflate(R.layout.fragment_toolbar, container, false);    Bundle arguments = getArguments();    if (arguments.getBoolean(TO_ADD_Feed_KEY)) {        Log.i(LOG_KEY, "Created Feed key");        Feed = addbutton(R.string.Feed, linearLayout);    }    if (arguments.getBoolean(TO_ADD_PROfile_KEY)) {        Log.i(LOG_KEY, "Created profile Key");        profile = addbutton(R.string.profile, linearLayout);    }    if (arguments.getBoolean(TO_ADD_FRIENDS_KEY)) {        Log.i(LOG_KEY, "Created frIEnds key");        frIEnds = addbutton(R.string.frIEnds, linearLayout);    }    logout = addbutton(R.string.logout, linearLayout);    return linearLayout;}@OverrIDepublic voID onClick(VIEw vIEw) {    Log.i(LOG_KEY, "A button was clicked.");    if (getActivity() instanceof IToolbarCallback) {        IToolbarCallback itc = (IToolbarCallback) getActivity();        if (vIEw.equals(Feed)) {            itc.Feed();        }        if (vIEw.equals(profile)) {            itc.profile();        }        if (vIEw.equals(frIEnds)) {            itc.frIEnds();        }        if (vIEw.equals(logout)) {            itc.logout();        }    }}

}

There’s no other code pertinent to this, besIDes the callback
interface.

public interface IToolbarCallback {voID Feed();voID profile();voID logout();voID frIEnds();

}

This is just used to let the host activity kNow what was clicked.
Finally, the XML:

<linearLayout 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"tools:context="project3.csc214.project23.Fragments.Toolbar.ToolbarFragment"androID:orIEntation="horizontal">

I use a builder for the fragment, and here’s the code:

public class ToolbarBuilder {private boolean addFeed;private boolean addProfile;private boolean addFrIEnds;private FragmentManager fragmentManager;private int addToID;public ToolbarBuilder(FragmentManager fragmentManager, int addToID) {    this.fragmentManager = fragmentManager;    this.addToID = addToID;    addFeed = false;    addProfile = false;    addFrIEnds = false;}public ToolbarBuilder addFeed() {    addFeed = true;    return this;}public ToolbarBuilder addProfile() {    addProfile = true;    return this;}public ToolbarBuilder addFrIEnds() {    addFrIEnds = true;    return this;}public voID build() {    Bundle bundle = new Bundle();    bundle.putBoolean(ToolbarFragment.TO_ADD_Feed_KEY, addFeed);    bundle.putBoolean(ToolbarFragment.TO_ADD_FRIENDS_KEY, addFrIEnds);    bundle.putBoolean(ToolbarFragment.TO_ADD_PROfile_KEY, addProfile);    ToolbarFragment toolbarFragment = new ToolbarFragment();    toolbarFragment.setArguments(bundle);    fragmentManager.beginTransaction().add(addToID, toolbarFragment).commit();}

}

只是为了澄清,据我所知,这些按钮没有收到任何输入.他们不仅没有打电话给onClick,而且根本没有做出任何反应.据我所知,onClick设置正确,按钮在某些基本级别上只是断开的.

情节已经扩大.在其他活动中使用完全相同的设置似乎可以使其正常工作……据我所知,没有任何更改.

无论如何,我决定仅对该活动进行硬编码,以便继续进行应用程序的其他部分.谢谢大家对问题的审议.如果我知道发生了什么,我会再次发布.

解决方法:

不要比较视图以查看它们是否相等,请比较ID.这里:

@OverrIDepublic voID onClick(VIEw vIEw) {    Log.i(LOG_KEY, "A button was clicked.");    if (getActivity() instanceof IToolbarCallback) {        IToolbarCallback itc = (IToolbarCallback) getActivity();        if (vIEw.equals(Feed)) {            itc.Feed();        }        if (vIEw.equals(profile)) {            itc.profile();        }        if (vIEw.equals(frIEnds)) {            itc.frIEnds();        }        if (vIEw.equals(logout)) {            itc.logout();        }    }}

它应该是:

@OverrIDepublic voID onClick(VIEw vIEw) {    Log.i(LOG_KEY, "A button was clicked.");    if (getActivity() instanceof IToolbarCallback) {        IToolbarCallback itc = (IToolbarCallback) getActivity();        if (vIEw.getID() == Feed.getID()) {            itc.Feed();        }        if (vIEw.getID() == profile.getID()) {            itc.profile();        }        if (vIEw.getID() == frIEnds.getID()) {            itc.frIEnds();        }        if (vIEw.getID() == logout.getID()) {            itc.logout();        }    }}

此外,当您自己创建视图时,还需要给它们提供ID.如果您的API级别为17,则只需在视图上调用generateVIEwID(),AndroID就会为您创建一个唯一的ID.
所以这样做:

private button addbutton(int stringID, linearLayout linearLayout) {    button button = new button(getContext());    button.setText(stringID);    button.setonClickListener(this);    linearLayout.addVIEw(button);    button.generateVIEwID();    return button;}

编辑:
除了上面指定的内容之外,您的代码看起来还不错.我想尝试的一件事是在addbutton方法外部设置侦听器:

private button addbutton(int stringID, linearLayout linearLayout) {    button button = new button(getContext());    button.setText(stringID);    linearLayout.addVIEw(button);    button.generateVIEwID();    return button;}if (arguments.getBoolean(TO_ADD_Feed_KEY)) {        Log.i(LOG_KEY, "Created Feed key");        Feed = addbutton(R.string.Feed, linearLayout);        Feed.setonClickListener(this);    }    if (arguments.getBoolean(TO_ADD_PROfile_KEY)) {        Log.i(LOG_KEY, "Created profile Key");        profile = addbutton(R.string.profile, linearLayout);        profile.setonClickListener(this);    }    if (arguments.getBoolean(TO_ADD_FRIENDS_KEY)) {        Log.i(LOG_KEY, "Created frIEnds key");        frIEnds = addbutton(R.string.frIEnds, linearLayout);        frIEnds.setonClickListener(this);    }    logout = addbutton(R.string.logout, linearLayout);    logout.setonClickListener(this);

如果这不起作用,请尝试检查按钮上方是否没有其他任何拦截点击事件的视图.

总结

以上是内存溢出为你收集整理的java-无法单击Android按钮全部内容,希望文章能够帮你解决java-无法单击Android按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1210061.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存