gpt4 book ai didi

android - onClickListener 无法解析为变量

转载 作者:行者123 更新时间:2023-11-29 21:10:11 25 4
gpt4 key购买 nike

我正在尝试对所有四个 ImageButton 实现相同的 onClickListener。但它显示未解析 onClickListener 的错误。

package com.yogeshbalan.myrootguide;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.yogeshbalan.myrootguide.fragment_1.Android_Rooting;
import com.yogeshbalan.myrootguide.fragment_1.How;
import com.yogeshbalan.myrootguide.fragment_1.What;
import com.yogeshbalan.myrootguide.fragment_1.Why;

public class MainActivity extends Activity {

Fragment fragment;
public ImageButton android_rooting,android_what,android_why,android_how;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ImageButton android_rooting = (ImageButton) findViewById(R.id.imageButton_androidrooting);
final ImageButton android_what = (ImageButton) findViewById(R.id.imageButton_what);
final ImageButton android_why = (ImageButton) findViewById(R.id.imageButton_why);
final ImageButton android_how = (ImageButton) findViewById(R.id.imageButton_how);

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

Android_Rooting myFragment = new Android_Rooting();
ft.add(R.id.myFragment, myFragment);
ft.commit();


android_rooting.setOnClickListener(onClickListener);
android_what.setOnClickListener(onClickListener);
android_why.setOnClickListener(onClickListener);
android_how.setOnClickListener(onClickListener);

ImageButton.OnClickListener onClickListener = new OnClickListener(){

Fragment newFragment;
@Override
public void onClick(View v) {

if (v.getId()==R.id.imageButton_androidrooting) {

newFragment = new Android_Rooting();

} else if(v.getId()==R.id.imageButton_what){

newFragment = new What();

}
else if(v.getId()==R.id.imageButton_why){

newFragment = new Why();

}
else if(v.getId()==R.id.imageButton_how){

newFragment = new How();

}

}

};

FragmentTransaction Transaction = getFragmentManager().beginTransaction();
Transaction.replace(R.id.myFragment, myFragment);
Transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
Transaction.commit();

}

我想在我的 MainActivity 中添加 Fragment。但它并不像我期望的那样。plaeas 帮助我......或建议另一种方法来做到这一点......我正在学习本教程 https://www.youtube.com/watch?v=KyXvq_kwfzg

最佳答案

尝试放置

android_rooting.setOnClickListener(onClickListener);
android_what.setOnClickListener(onClickListener);
android_why.setOnClickListener(onClickListener);
android_how.setOnClickListener(onClickListener);

低于

ImageButton.OnClickListener onClickListener = new OnClickListener(){
...
};

希望对你有帮助

关于android - onClickListener 无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23168229/

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