java-无法单击Android按钮
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-无法单击Android按钮,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6196字,纯文字阅读大概需要9分钟。
内容图文
![java-无法单击Android按钮](/upload/InfoBanner/zyjiaocheng/669/278461ab07e14d388cfe09e43552055e.jpg)
我正在创建一个应用程序,该应用程序在顶部使用带有一些按钮的片段之类的工具栏.按钮以编程方式添加.
问题是,当我启动该应用程序时,会出现工具栏和按钮,但是无法单击按钮.当将鼠标悬停在按钮上(我使用模拟器)或单击按钮时,它们根本不会改变,也不会通知我的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;
}
@Override
public 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;
}
@Override
public 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.这里:
@Override
public 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();
}
}
}
它应该是:
@Override
public 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按钮所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。