2 回答
TA贡献1891条经验 获得超3个赞
您需要为框架声明按钮和视图像这样
Button button;
public View myView;
然后在 onCreate 像这样初始化
myView = inflater.inflate(R.layout.fragment_button,container,false);
button = (Button) myView.findViewById(R.id.button_next_trans);
然后创建这样的方法,
public void payments(){
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),MainActivity.class)
startActivity(i);
}
}
}
并在 onCreate() 方法结束时返回视图。
return myView;
并调用支付();在 onCreate
TA贡献1802条经验 获得超5个赞
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_button);
Button btnPay = (Button)findViewById(R.id.button_next_trans);
btnPay.setOnClickListener(this);
}
public void goToPayments(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
@Override
public void onClick(View v) {
{
switch (v.getId()) {
case R.id.button_next_trans:
goToPayments();
break;
}
}
}}
从 xml 中删除以下行
android:onClick="goToPayments"
添加回答
举报