Java onClick事件片段
我对片段中的onClick事件有一些问题。 我在Actionbar中进行选项卡导航,并使用TablListener更改片段。 这个很好用 我的问题是“正常”onclick侦听器(见下文)不起作用。 我尝试了很多不同的方法。以下是当前状态:Java onClick事件片段,java,android,android-fragments,Java,Android,Android Fragments,我对片段中的onClick事件有一些问题。 我在Actionbar中进行选项卡导航,并使用TablListener更改片段。 这个很好用 我的问题是“正常”onclick侦听器(见下文)不起作用。 我尝试了很多不同的方法。以下是当前状态: package com.artifact.actionbarfragments; import android.app.ActionBar; import android.app.ActionBar.Tab; import andro
package com.artifact.actionbarfragments;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class ActionBarFragmentsActivity extends Activity {
/** Called when the activity is first created. */
public OnClickListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// lädt die ActionBar
ActionBar actionbar = getActionBar();
// ActionBar.LayoutParams(0);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayShowCustomEnabled(false);
// actionbar.setCustomView(R.layout.actionbarview);
// lädt die Tabs in die Actionbar und setzt ''eigene'' Icons
ActionBar.Tab TabA = actionbar.newTab().setIcon(R.drawable.ic_action_view_as_list);
ActionBar.Tab TabB = actionbar.newTab().setIcon(R.drawable.ic_action_directions);
ActionBar.Tab TabC = actionbar.newTab().setIcon(R.drawable.ic_action_group);
ActionBar.Tab TabD = actionbar.newTab().setIcon(R.drawable.ic_action_person);
// erstellt neue Fragmente
Fragment fragmentA = new fragmentA();
Fragment fragmentB = new fragmentB();
Fragment fragmentC = new fragmentC();
Fragment fragmentD = new fragmentD();
// Listener werden angelegt
TabA.setTabListener(new MyTabsListener(fragmentA));
TabB.setTabListener(new MyTabsListener(fragmentB));
TabC.setTabListener(new MyTabsListener(fragmentC));
TabD.setTabListener(new MyTabsListener(fragmentD));
// Tabs werden in die actionbar geladen
actionbar.addTab(TabA);
actionbar.addTab(TabB);
actionbar.addTab(TabC);
actionbar.addTab(TabD);
}
}
class NotfallButtonListener extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.id.fragment_container);
final Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
notfallbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notfallbtn.setText("Notfall ausgelöst");
System.out.println("Notfall Botton gedrückt");
}
});
}
}
当上述过程不起作用时,我尝试将onClick类保留在片段中:
public class fragmentA extends Fragment
implements View.OnClickListener
{
Button notfallbtn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// das Layout fuer dieses Fragment laden
return inflater.inflate(R.layout.fragmenta, container, false);
// Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
// notfallbtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
class NotfallButtonListener extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.id.fragment_container);
final Button notfallbtn = (Button) findViewById(R.id.notfallbtn);
notfallbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notfallbtn.setText("Notfall ausgelöst");
System.out.println("Notfall Botton gedrückt");
}
});
}
}
}
}
任何帮助都将不胜感激!非常感谢 考虑到按钮位于
fragmneta.xml
中,请按如下所示进行更改
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// das Layout fuer dieses Fragment laden
View view= inflater.inflate(R.layout.fragmenta, container, false);
// inflate layout
Button notfallbtn = (Button) view.findViewById(R.id.notfallbtn);
// initialize button using the inflated view object
notfallbtn.setOnClickListener(this);
// listener for button
return view; // return inflated view
}
那你已经有了
public class fragmentA extends Fragment
implements View.OnClickListener
所以
把这些都去掉
@Override
public void onClick(View view) {
class NotfallButtonListener extends Activity {
伟大的谢谢,行得通。一个小问题。如何更改文本视图(id TextView)等<代码>。。。case R.id.notfallbtn:textView.setText(“按钮按下”)@johnbob在onCreateView中初始化textView,就像按钮一样。但是在创建视图上声明textview。然后在按钮上单击将文本设置为文本视图OK初始化文本视图:
textview textview=(textview)view.findViewById(R.id.textview)代码>不理解2。部分:/SryTextView TextView@覆盖createview上的公共视图(LayoutInflater充气机、ViewGroup容器、Bundle savedInstanceState){
然后在onCreateView
中是TextView=(TextView)View.findViewById(R.id.TextView);
thencase R.id.notfallbtn:TextView.settxt(“你好”);中断;
@Override
public void onClick(View view) {
class NotfallButtonListener extends Activity {