gpt4 book ai didi

android - setContentView 和 EditText

转载 作者:行者123 更新时间:2023-11-29 22:15:57 25 4
gpt4 key购买 nike

我有两个布局 xml,但无法使第二个布局正常工作。放置在第二个布局上的 EditText 无法按预期工作。它不接受字符。

我在这里错过了什么?我应该改用 startActivity() 吗?

主.java

public class Main extends Activity implements View.OnClickListener {

EditText box1, box2;

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
showXml1();
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
String box11 = box1.getText().toString();
Toast.makeText(this, box11,Toast.LENGTH_SHORT).show();
showXml2();
break;
case R.id.button2:
String box22 = box2.getText().toString();
Toast.makeText(this, box22,Toast.LENGTH_SHORT).show();
showXml1();
break;
}
}

public void showXml2() {
setContentView(R.layout.main2);
box2 = (EditText) findViewById(R.id.editText2);
}
public void showXml1() {
setContentView(R.layout.main);
box1 = (EditText) findViewById(R.id.editText1);
}
}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main1" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:onClick="onClick"
/>

</LinearLayout>

mail2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Main2" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:onClick="onClick"
/>
</LinearLayout>

最佳答案

使用结构类似于此的元布局 xml 文件:

meta_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root">
<include
layout="@layout/main" />
<include
layout="@layout/main2" />
</ViewFlipper>

然后是main.java:

public class Main extends Activity implements View.OnClickListener {

EditText box1, box2;
ViewFlipper root;

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.meta_main);
box1 = (EditText) findViewById(R.id.editText1);
box2 = (EditText) findViewById(R.id.editText2);
root = (ViewFlipper) findViewById(R.id.root);
}

public void onClick(View v) {
EditText editText = null;
switch (v.getId()) {
case R.id.button1:
editText = box1;
root.showNext();
break;
case R.id.button2:
editText = box2;
root.showPrevious();
break;
}
if(editText != null) {
Toast.makeText(this, editText.getText(), Toast.LENGTH_SHORT).show();
}
}
}

希望对您有所帮助;)

关于android - setContentView 和 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8447922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com