所以我刚刚开始尝试学习安卓代码,作为第一次体验,我想添加2个窗口/布局以及从一个到另一个的方式,我认为2个插件可以解决问题。
所以基本上我从activity_my开始,当我按下这个UI中的按钮时,我会进入名为layout2的第二个布局,但是当我尝试回到原来的那个时,没有任何事情发生..为什么?
public class MyActivity extends Activity {
int k =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
final Button button2 = (Button) findViewById(R.id.button);
EditText phone = (EditText) findViewById(R.id.phoneNUMBER);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.layout2);
k=1;
}
});
if(k==1){
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
k=0;
setContentView(R.layout.activity_my);
}
});
}
答案 0 :(得分:0)
您可以使用一个布局和一个活动。你必须在按钮click.i实现它时隐藏/显示你的布局。你可以尝试下面的代码。可能它会帮助你。
<强> main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20"
android:gravity="center_horizontal"
android:text="layout 1" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="layout 1" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="layout 2" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="layout 2" />
</LinearLayout>
<强> MyActivity.java 强>
public class MyActivity extends Activity {
Button btn1, btn2;
LinearLayout layout1, layout2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
layout1 = (LinearLayout) findViewById(R.id.layout1);
layout2 = (LinearLayout) findViewById(R.id.layout2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
layout1.setVisibility(View.VISIBLE);
layout2.setVisibility(View.GONE);
}
});
}
}