【问题标题】:How to add Branch window to Main window in android?如何在android中将分支窗口添加到主窗口?
【发布时间】:2013-08-06 11:52:33
【问题描述】:

我在 android 上创建了一个 main.xml 窗口,在其中我有几个按钮,现在我创建了另一个 Sound.xml 窗口,那么如何在按下第一个窗口 (main.xml) 按钮时打开窗口.

在主 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);
        }
    });

在第二个窗口

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>

【问题讨论】:

  • 您可以像大多数情况下那样通过意图来执行此操作。请参阅以下链接。确保您已将第二个 stivity 添加到清单中。见以下链接stackoverflow.com/questions/4186021/…
  • 是的,我已经浏览了您的链接。它帮助我解决了我的问题。
  • 总是乐于提供帮助:)

标签: android xml button


【解决方案1】:

主窗口(活动)

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;
}*/

}

第二个窗口(活动)

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;
    }
}   
}

主祭

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

        ></activity>

在解决这个问题后,我能够解决我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    相关资源
    最近更新 更多