Java 尝试使用2个不同的按钮打开2个不同的xml页面
我编写了以下代码:Java 尝试使用2个不同的按钮打开2个不同的xml页面,java,android,xml,eclipse,button,Java,Android,Xml,Eclipse,Button,我编写了以下代码: public class FragmentFirstPage extends Fragment implements OnClickListener{ View root; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){ root = inflater.inflate(R.layout.fragment_page1
public class FragmentFirstPage extends Fragment implements OnClickListener{
View root;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){
root = inflater.inflate(R.layout.fragment_page1, container, false);
Button bt = (Button) root.findViewById(R.id.button1);
bt.setOnClickListener(this);
Button mbt = (Button) root.findViewById(R.id.imageButton1);
mbt.setOnClickListener(new View.OnClickListener() {
final ViewPager viewPager = (ViewPager) getActivity().findViewById (R.id.activity_main_viewpager);
@Override
public void onClick (View v){
viewPager.setCurrentItem (viewPager.getCurrentItem() + 1); //or -1 to go previous
}
});
return root;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getActivity(), SplashActivity.class);
getActivity().startActivity(myIntent);
}
当我运行它时,它不会显示任何错误,但也不会工作。它显示一条错误消息,表示不幸的是,应用程序已停止工作。我应该做些什么来正确处理我的xml页面有两个按钮button1打开一个活动,imagebutton1显示下一个片段。这就是我想要实现的。您只需要为所有按钮设置
onClickListener
,然后在if else
或开关
案例中比较它们的ID,然后做任何您想做的事情
例如
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button1)
{
Intent i=new Intent(beforemain.this, Splash1.class);
startActivity(i);
}
else if(v.getId()==R.id.button2)
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);}
}
}
您只需为所有按钮设置
onClickListener
,然后在if-else
或开关
案例中比较它们的ID,然后做任何您想做的事情
例如
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button1)
{
Intent i=new Intent(beforemain.this, Splash1.class);
startActivity(i);
}
else if(v.getId()==R.id.button2)
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);}
}
}
您只需为所有按钮设置
onClickListener
,然后在if-else
或开关
案例中比较它们的ID,然后做任何您想做的事情
例如
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button1)
{
Intent i=new Intent(beforemain.this, Splash1.class);
startActivity(i);
}
else if(v.getId()==R.id.button2)
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);}
}
}
您只需为所有按钮设置
onClickListener
,然后在if-else
或开关
案例中比较它们的ID,然后做任何您想做的事情
例如
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button1)
{
Intent i=new Intent(beforemain.this, Splash1.class);
startActivity(i);
}
else if(v.getId()==R.id.button2)
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);}
}
}
您只需在
onClick中检查通过检查调用视图的id单击的按钮。从那里你可以决定每个人将做什么
public class beforemain extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
Button mBtn2 = (Button) findViewById(R.id.button2); //Just like #1
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this); //Also like #1
}
@Override
public void onClick(View v) {
switch(v.getId()) { //Get the id of the button that was clicked
case R.id.button1:
Intent i = new Intent(beforemain.this, Splash1.class);
startActivity(i);
break;
case R.id.button2:
Intent i = new Intent(beforemain.this, Splash2.class);
startActivity(i);
break;
}
}
您只需在onClick中检查通过检查调用视图的id单击的按钮。从那里你可以决定每个人将做什么
public class beforemain extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
Button mBtn2 = (Button) findViewById(R.id.button2); //Just like #1
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this); //Also like #1
}
@Override
public void onClick(View v) {
switch(v.getId()) { //Get the id of the button that was clicked
case R.id.button1:
Intent i = new Intent(beforemain.this, Splash1.class);
startActivity(i);
break;
case R.id.button2:
Intent i = new Intent(beforemain.this, Splash2.class);
startActivity(i);
break;
}
}
您只需在onClick中检查通过检查调用视图的id单击的按钮。从那里你可以决定每个人将做什么
public class beforemain extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
Button mBtn2 = (Button) findViewById(R.id.button2); //Just like #1
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this); //Also like #1
}
@Override
public void onClick(View v) {
switch(v.getId()) { //Get the id of the button that was clicked
case R.id.button1:
Intent i = new Intent(beforemain.this, Splash1.class);
startActivity(i);
break;
case R.id.button2:
Intent i = new Intent(beforemain.this, Splash2.class);
startActivity(i);
break;
}
}
您只需在onClick中检查通过检查调用视图的id单击的按钮。从那里你可以决定每个人将做什么
public class beforemain extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
Button mBtn2 = (Button) findViewById(R.id.button2); //Just like #1
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this); //Also like #1
}
@Override
public void onClick(View v) {
switch(v.getId()) { //Get the id of the button that was clicked
case R.id.button1:
Intent i = new Intent(beforemain.this, Splash1.class);
startActivity(i);
break;
case R.id.button2:
Intent i = new Intent(beforemain.this, Splash2.class);
startActivity(i);
break;
}
}
将onClick方法的实现更改如下
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
mBtn1.setOnClickListener(this);
Button mBtn2 = (Button) findViewById(R.id.button1);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mBtn1.getId()==v.getID())
{
Intent i1=new Intent(beforemain.this, Splash1.class);
startActivity(i1);
}
else
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);
}
}
将onClick方法的实现更改如下
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
mBtn1.setOnClickListener(this);
Button mBtn2 = (Button) findViewById(R.id.button1);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mBtn1.getId()==v.getID())
{
Intent i1=new Intent(beforemain.this, Splash1.class);
startActivity(i1);
}
else
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);
}
}
将onClick方法的实现更改如下
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
mBtn1.setOnClickListener(this);
Button mBtn2 = (Button) findViewById(R.id.button1);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mBtn1.getId()==v.getID())
{
Intent i1=new Intent(beforemain.this, Splash1.class);
startActivity(i1);
}
else
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);
}
}
将onClick方法的实现更改如下
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
mBtn1.setOnClickListener(this);
Button mBtn2 = (Button) findViewById(R.id.button1);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mBtn1.getId()==v.getID())
{
Intent i1=new Intent(beforemain.this, Splash1.class);
startActivity(i1);
}
else
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);
}
}
没问题,很高兴能帮你。好的,一切正常,但当我尝试添加一个声音池时,它从不播放曲目。嗨;你能不能再看一遍这个问题?我刚刚更新了它。你能帮我解决我的问题吗?没问题,很高兴能帮你解决。好的,一切正常,但当我尝试添加一个声音池时,它从不播放曲目。嗨;你能不能再看一遍这个问题?我刚刚更新了它。你能帮我解决我的问题吗?没问题,很高兴能帮你解决。好的,一切正常,但当我尝试添加一个声音池时,它从不播放曲目。嗨;你能不能再看一遍这个问题?我刚刚更新了它。你能帮我解决我的问题吗?没问题,很高兴能帮你解决。好的,一切正常,但当我尝试添加一个声音池时,它从不播放曲目。嗨;请你再看一遍这个问题好吗?我刚刚更新了它。你能帮我解决我的问题吗。