动态Xml(我认为)

时间:2011-08-09 17:05:09

标签: java android eclipse android-layout

编辑:谢谢大家的帮助。我编辑了我的Database类以包含以下

    final EditText firstName = (EditText) findViewById(R.id.editText1);  // First Name
    final EditText middleName = (EditText) findViewById(R.id.editText2);  // Middle Name
    final EditText birthDate = (EditText) findViewById(R.id.editText4);  // Birth Date

    final String firstname = firstName.getText().toString();  // First Name
    final String middlename = middleName.getText().toString();  // Middle Name
    final String birthdate = birthDate.getText().toString();  // Birth Date

    TextView firstNameText = (TextView)findViewById(R.id.firstname);
    TextView middleNameText = (TextView)findViewById(R.id.middlename);
    TextView birthDateText = (TextView)findViewById(R.id.birthdate);

    firstNameText.setText(firstname);
    middleNameText.setText(middlename);
    birthDateText.setText(birthdate);

我的database.xml现在显示

 <TableRow
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:background="#aa0000"            
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:id="@+id/firstname"/>
    <TextView
        android:background="#00aa00"
        android:gravity="center_horizontal" 
        android:layout_span="3"
        android:id="@+id/middlename"/>
    <TextView
        android:background="#0000aa"          
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:id="@+id/birthdate"/>
    </TableRow>

但是当我运行模拟器并尝试访问该屏幕时(通过单击上一个屏幕中的按钮)应用程序崩溃了?我使用OnClickListener正确设置了按钮,所以我很确定按钮不是问题

3 个答案:

答案 0 :(得分:1)

尝试以下

final EditText firstName = (EditText) findViewById(R.id.editText1);  
TextView firstNameTxt = (TextView)findViewById(R.id.firstname); 
firstNameTxt.setText(firstName);

答案 1 :(得分:1)

如果您在理解界面时遇到问题,可以剪切并粘贴。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="First Name: "
    android:id="@+id/firstLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"
    />
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/firstEdit" 
    android:text="John"
    android:layout_toRightOf="@id/firstLabel"
    android:layout_alignParentRight="true"></EditText>
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Middle Name: "
    android:id="@+id/middleLabel"
    android:layout_below="@id/firstLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"/>
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/middleEdit" 
    android:text="Phillip"
    android:layout_below="@id/firstEdit"
    android:layout_toRightOf="@id/middleLabel"
    android:layout_alignParentRight="true"></EditText>
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Last Name: "
    android:id="@+id/lastLabel"
    android:layout_below="@id/middleLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"
    />
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/lastEdit" 
    android:text="Doe"
    android:layout_below="@id/middleEdit"
    android:layout_toRightOf="@id/lastLabel"
    android:layout_alignParentRight="true"></EditText>
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Birthdate: "
    android:id="@+id/birthLabel"
    android:layout_below="@id/lastLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"
    />
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/birthEdit" 
    android:text="08/09/1977"
    android:layout_toRightOf="@id/lastLabel"
    android:layout_below="@id/lastEdit"
    android:layout_alignParentRight="true"></EditText>


<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/birthEdit">
    <TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <TextView
        android:background="#aa0000"            
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:text="something"
        android:id="@+id/firstTable"/>
    <TextView
        android:background="#00aa00"
        android:gravity="center_horizontal" 
        android:layout_span="3"
        android:text="something1"
        android:id="@+id/middleTable"/>
    <TextView
        android:background="#0000aa"          
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:text="something2"
        android:id="@+id/birthTable"/>
    </TableRow>
    </LinearLayout>
    </RelativeLayout>

DatabaseExample.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class DatabaseExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Setup edit fields
        EditText firstEdit = (EditText)findViewById(R.id.firstEdit);
        EditText middleEdit = (EditText)findViewById(R.id.middleEdit);
        EditText lastEdit = (EditText)findViewById(R.id.lastEdit);
        EditText birthEdit = (EditText)findViewById(R.id.birthEdit);


        //Get the text and store in variables
        String firstName = firstEdit.getText().toString();
        String middleName = middleEdit.getText().toString();
        String lastName = lastEdit.getText().toString();
        String birthDate = birthEdit.getText().toString();

        //setup the text fields
        TextView firstTable = (TextView)findViewById(R.id.firstTable);
        TextView middleTable = (TextView)findViewById(R.id.middleTable);
        TextView birthTable = (TextView)findViewById(R.id.birthTable);

        //change the text fields
        firstTable.setText(firstName);
        middleTable.setText(middleName);
        birthTable.setText(birthDate);


    }
}

这再一次只是输入和显示数据的界面。您可以使用SQLite,SharedPreferences或写入SDCard上的文件,而不是将数据存储在String变量中。

答案 2 :(得分:0)

您无法在strings.xml文件中存储用户的值。您必须拥有SharedPreferencesSQLite. Here等数据存储,这是另一个有关Android数据存储帮助的链接。