我需要执行onClick来调用另一个类的onItemSelected侦听器方法.我不知道如何在Image按钮的onClick侦听器中调用该方法,因此它将移至HomeFirstFragment类.
ItmeSelectedListener
public interface ItemSelectedListener { public voID onItemSelected(final int position, final String content);}
LayoutActivity.java:
public class LayoutActivity extends Activity implements OnClickListener {Imagebutton btn_click; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_grID3); btn_click = (Imagebutton) findVIEwByID(R.ID.btn_click); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.btn_click: break; } }}
MainActivity.java:
public class MainActivity extends ActionBaractivity implements OnTabchangelistener,ItemSelectedListener { private TextVIEw action_bar_hometext; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main);} @OverrIDe public voID onItemSelected(int position, String content) { if(position==0) { action_bar_hometext.setText(content); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction ft = manager.beginTransaction(); HomeFirstFragment newFragment = new HomeFirstFragment(); ft.replace(R.ID.realtabcontent, newFragment); ft.addToBackStack(null); ft.commit(); } }
如果我单击LayoutActivity类中的btn_click,则需要执行onClick以从MainActivity调用接口方法.
任何人都可以帮助我.谢谢.
解决方法:
您可以创建自己的侦听器,并在您自己的侦听器中单击要添加的代码块.
创建你的界面像
Interface MyListener{ public voID myClickListener(String content);}
现在在您的MainActivity中实现
public class MainActivity extends ActionBaractivity implements OnTabchangelistener,ItemSelectedListener,MyListener { public voID myClickListener(String content){ action_bar_hometext.setText(content); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction ft = manager.beginTransaction(); HomeFirstFragment newFragment = new HomeFirstFragment(); ft.replace(R.ID.realtabcontent, newFragment); ft.addToBackStack(null); ft.commit(); }}
总结 以上是内存溢出为你收集整理的java-onClick从MainActivity调用接口方法全部内容,希望文章能够帮你解决java-onClick从MainActivity调用接口方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)