[英]How to add Branch window to Main window in android?
我在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>
主窗口(活動)
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>
完成此工作后,我就能解決我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.