简体   繁体   English

如何在Android的主窗口中添加分支窗口?

[英]How to add Branch window to Main window in android?

I created a main.xml window on android in which I have taken a couple of button and now I created another Sound.xml window, so how can I open the window on the press of first window ( main.xml ) button. 我在android上创建了一个main.xml窗口,在其中我单击了几个按钮,现在又创建了另一个Sound.xml窗口,因此如何按第一个窗口( main.xml )按钮来打开该窗口。

On main java. 在主Java上。

public class Main extends Activity {




@Override
protected void onCreate(Bundle savedInstanceState) {

   /** Called when the activity is first created. */

     super.onCreate(savedInstanceState);

     setContentView(R.layout.cyk_main);

     Button orderButton = (Button) findViewById(R.id.options_btn);
    orderButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Main.this,soundLayout.class);
            startActivity(intent);
        }
    });

on second window 在第二个窗口

public class soundLayout extends Activity {


@Override

  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soundLayout);
Button orderButton = (Button) findViewById(R.id.btn_finish_dialog);
orderButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    finish();
}
});

} }

<activity  
        android:name=".SoundOptionLayout
"android:theme="@android:style/Theme.Dialog" >
    </activity>

Main Window(activity) 主窗口(活动)

package com.myproject.testprojectonintent;
import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{



/** the button that I've added in the xml*/
Button startBtn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_screen);

    /** Get the id of the button from the xml*/
    startBtn = (Button) findViewById(R.id.startBtn);
    startBtn.setOnClickListener( this);
}


@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    /** make a refernce to store the intent when the view has been clicked*/
    Intent intent;

    /** Make cases according to the number of buttons you have in screen
     * In this case, I've added one.*/
    switch(view.getId()){

    case R.id.startBtn :
        Log.i("btnClick","Start button id is"+view.getId());
        /** Intent should be fired from this activity to the other activity*/
        intent = new Intent(MainActivity.this, ResultActivity.class);
        /** This is useful when you want to get the button that is clicked here on the 
         * destination activity*/
        intent.putExtra("button_click", "Start");
        /** Start the intent*/
        startActivity(intent);

        /*** Use this only when you want to finish your current activity while opening an another one.
         * if this is commented, then this activity will be running in the background.
        ***/
        this.finish();
        break;
    }

}

/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_screen, menu);
    return true;
}*/

} }

Second Window(activity) 第二窗口(活动)

package com.myproject.testprojectonintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
public class ResultActivity extends Activity implements OnClickListener{

Button resultBtn;

private String _showText;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_screen);

    /** Fetched from the MAIN ACTIVITY */
    Bundle bundle = getIntent().getExtras();
    if(bundle!=null){
        _showText = bundle.getString("button_click");
    }
    Log.i("btnClick","button clicked is :"+_showText);


    resultBtn = (Button) findViewById(R.id.resultBtn);
    resultBtn.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    /** make a refernce to store the intent when the view has been clicked*/
    Intent intent;

    /** Make cases according to the number of buttons you have in screen
     * In this case, I've added one.*/
    switch(view.getId()){

    case R.id.resultBtn :
        Log.i("btnClick","Result button is clicked whose id is :"+view.getId());
        /** Intent should be fired from this activity to the other activity*/
        intent = new Intent(ResultActivity.this, MainActivity.class);

        /** Start the intent*/
        startActivity(intent);
        this.finish();

        break;
    }
}   
}

Mainfest 维护节

 <activity 
        android:name="com.myproject.testprojectonintent.ResultActivity"

        ></activity>

After working on this I am able to Solve my problem. 完成此工作后,我就能解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM