gpt4 book ai didi

android - 如何在android fragment 中点击按钮运行代码

转载 作者:行者123 更新时间:2023-11-29 14:23:05 24 4
gpt4 key购买 nike

我需要帮助才能使应用正常运行。我使用 fragment 设置了一个多选项卡界面(因此它有 4 个具有不同内容的选项卡)在我正在处理的 fragment 上我想要有 3 个按钮来运行这样的 Intent :

    Log.i(TAG, "Website Clicked");
Intent websiteBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/"));
startActivity(websiteBrowserIntent);

所以我有 3 个按钮,这是 .xml:

由于某种原因,XML 似乎不起作用这里是一个 pastebin: http://pastebin.com/gGuh7qb2

这里是 TopRatedFragment.java:

package com.bordengrammar.bordengrammarapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TopRatedFragment extends Fragment {

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

View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

return rootView;
}
}

最佳答案

public class TopRatedFragment extends Fragment implements OnClickListener {

Button btn;

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

View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

btn = (Button) rootView.findViewById(R.id.myButton);
btn.setOnClickListener(this);
return rootView;
}

@Override
public void onClick(View v) {
// implements your things
}
}

附言稍作搜索即可获得答案。

[编辑]

public class TopRatedFragment extends Fragment implements OnClickListener {

Button btn;

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

View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

btn = (Button) rootView.findViewById(R.id.myButton);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {



}
});
return rootView;
}

请阅读一些教程 - http://www.vogella.com/articles/AndroidFragments/article.html

关于android - 如何在android fragment 中点击按钮运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20573971/

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