多个按钮在片段类问题? [英] Multiple Buttons In Fragment Class Issue?
问题描述
我有一个问题,成功与开辟新的活动
从这些多个按钮。进出口新的编码,虽然。谁能帮
我这个问题。
我用的片段,我不能去上班,请帮助。
这里是我的code为止。
谢谢
公共类CreditFragment1扩展片段{
私有静态最后查看查看= NULL;
按钮的平衡;
按钮充值;
按钮份额;
按钮购买;
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
返回(的LinearLayout)inflater.inflate(R.layout.credit_frag1_layout,
集装箱,FALSE); //下一行下面我得到无法到达code
。余额=(按钮)getView()findViewById(R.id.balance_bt);
充值=(按钮)getView()findViewById(R.id.recharge_bt)。
份额=(按钮)getView()findViewById(R.id.share_bt)。
买=(按钮)getView()findViewById(R.id.buy_bt)。
OnClickListener balanceListener =新OnClickListener(){
公共无效的onClick(查看视图){
的setContentView(R.layout.balance_layout);
意图BalanceIntent =新意图(getActivity(),BalanceActivity.class);
startActivity(BalanceIntent);
}
私人无效的setContentView(INT balanceLayout){
// TODO自动生成方法存根 }
}; OnClickListener rechargeListener =新OnClickListener(){
公共无效的onClick(查看视图){
的setContentView(R.layout.recharge_layout);
意图BalanceIntent =新意图(getActivity(),RechargeActivity.class);
startActivity(BalanceIntent);
}
私人无效的setContentView(INT rechargeLayout){
// TODO自动生成方法存根 }
};
OnClickListener shareListener =新OnClickListener(){
公共无效的onClick(查看视图){
的setContentView(R.layout.share_layout);
意图BalanceIntent =新意图(getActivity(),ShareActivity.class);
startActivity(BalanceIntent);
}
私人无效的setContentView(INT shareLayout){
// TODO自动生成方法存根 }
}; OnClickListener buyListener =新OnClickListener(){
公共无效的onClick(查看视图){
的setContentView(R.layout.buy_layout);
意图BalanceIntent =新意图(getActivity(),BuyActivity.class);
startActivity(BalanceIntent);
}
私人无效的setContentView(INT buyLayout){
// TODO自动生成方法存根 }
};
balance.setOnClickListener(balanceListener);
recharge.setOnClickListener(rechargeListener);
share.setOnClickListener(shareListener);
buy.setOnClickListener(buyListener);
返回查看;
}
}
修改 onCreateView
到
公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.credit_frag1_layout,集装箱,FALSE);
余额=(按钮)view.findViewById(R.id.balance_bt);
balance.setOnClickListener(本);
//初始化类似的其他按钮
返回视图。
}
此外,您的类可以实现 OnClickListener
公共类CreditFragment1扩展片段实现OnClickListener {
在onCreateView
balance.setOnClickListener(本);
然后重写的onClick
。
@覆盖
公共无效onClikc(视图V)
{
开关(v.getId())
{
案例R.id.balance_bt:
//平衡按钮点击
打破;
案例R.id.recharge_bt
//充值按钮点击:
打破;
//同样,对于其它按钮
}
}
另外,我不知道你正在尝试与的setContentView(R.layout.balance_layout)做;
在OnClickListener。删除的setContentView(R.layout.balance_layout);
如果您需要导航到不同的活动中使用 startActivtiy(意向)
I am Having an Issue Succesfully opening new activities from these multiple buttons. Im new to coding though. can someone help me with this issues..
I used fragments and I cannot to get to work please help. Here is my code so far. Thank you
public class CreditFragment1 extends Fragment {
private static final View View = null;
Button balance;
Button recharge;
Button share;
Button buy;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return (LinearLayout) inflater.inflate(R.layout.credit_frag1_layout,
container, false);
// at the next line below i get "Unreachable code"
balance = (Button) getView().findViewById(R.id.balance_bt);
recharge = (Button) getView().findViewById(R.id.recharge_bt);
share = (Button) getView().findViewById(R.id.share_bt);
buy = (Button) getView().findViewById(R.id.buy_bt);
OnClickListener balanceListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.balance_layout);
Intent BalanceIntent = new Intent(getActivity(),BalanceActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int balanceLayout) {
// TODO Auto-generated method stub
}
};
OnClickListener rechargeListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.recharge_layout);
Intent BalanceIntent = new Intent(getActivity(),RechargeActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int rechargeLayout) {
// TODO Auto-generated method stub
}
};
OnClickListener shareListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.share_layout);
Intent BalanceIntent = new Intent(getActivity(),ShareActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int shareLayout) {
// TODO Auto-generated method stub
}
};
OnClickListener buyListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.buy_layout);
Intent BalanceIntent = new Intent(getActivity(),BuyActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int buyLayout) {
// TODO Auto-generated method stub
}
};
balance.setOnClickListener(balanceListener);
recharge.setOnClickListener(rechargeListener);
share.setOnClickListener(shareListener);
buy.setOnClickListener(buyListener);
return View;
}
}
Change onCreateView
to
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.credit_frag1_layout,container, false);
balance = (Button)view.findViewById(R.id.balance_bt);
balance.setOnClickListener(this);
// similarly initialize other buttons
return view;
}
Also your class can implement OnClickListener
public class CreditFragment1 extends Fragment implements OnClickListener {
In onCreateView
balance.setOnClickListener(this);
Then override onClick
.
@Override
public void onClikc(View v)
{
switch(v.getId())
{
case R.id.balance_bt :
// balance button clicked
break;
case R.id.recharge_bt
// recharge button clicked :
break;
// similarly for other buttons
}
}
Also i am not sure what you are trying to do with setContentView(R.layout.balance_layout);
in OnClickListener. Remove setContentView(R.layout.balance_layout);
If you need to navigate to a different Activity use startActivtiy(intent)
这篇关于多个按钮在片段类问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!