Android -dynamical textview创建活动

时间:2014-09-02 08:19:38

标签: android android-activity

使用editText创建了一个主活动,通过单击按钮,在第二个活动中创建了TextView,并将文本传递给它。现在我将回到第一个活动并提供一些新数据,以便新数据也与旧数据一起显示。但在我的情况下,不是添加,而是显示与旧数据重叠的新页面(包含新数据)。作为初学者,我尝试了我的水平,但我找不到任何解决方案。需要一些帮助...

这是我的主要活动

public class MainActivity extends Activity 
{

EditText txt1;
String value1;
Button button_1;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt1=(EditText)findViewById(R.id.editText1);
    value1=txt1.getText().toString();

    button_1=(Button)findViewById(R.id.button1);
    button_1.setOnClickListener(onClick());


}

private OnClickListener onClick() 
{
    // TODO Auto-generated method stub
    return new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent i=new Intent(getApplicationContext(),Activity2.class);
            i.putExtra("v",txt1.getText().toString());
            startActivity(i);

        }
    };
}

@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, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

我的第二个活动

public class Activity2 extends MainActivity
{
LinearLayout ll;
TextView txtview;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);

    ll=(LinearLayout)findViewById(R.id.linear);

    txtview=new TextView(this);
    txtview.setText(getIntent().getExtras().getString("v"));



    //txtview.setText(String.valueOf(getIntent().getExtras().getString("1v")));
    ll.addView(createNewTextView(txtview.getText().toString()));

    Button exit=(Button)findViewById(R.id.button1);
    exit.setOnClickListener(new Button.OnClickListener()
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            finish();
        }
    });
    Button New=(Button)findViewById(R.id.button2);
    New.setOnClickListener(new Button.OnClickListener()
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub

            Intent i=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);
        }
    });
}
private View createNewTextView(String text) 
{
    // TODO Auto-generated method stub
    final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    final TextView txtview=new TextView(this);
    txtview.setLayoutParams(lp);
    txtview.setText("New text: "+text);
    return txtview;
}
}

activity_main.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.self.MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="text"
    android:text="@string/e" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignRight="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:text="@string/app_name" />

activity2.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/linear"
    android:layout_width="match_parent"
    android:layout_height="418dp"
    android:orientation="vertical" >

</LinearLayout>

<LinearLayout
    android:id="@+id/hl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button2"
        android:layout_width="154dp"
        android:layout_height="wrap_content"
        android:text="@string/New" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit" />

</LinearLayout>

AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentmarklist"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".act2"></activity>
</application>

2 个答案:

答案 0 :(得分:0)

你应该看看android中的life-cycle of activities。大多数情况下,活动不会被销毁并再次使用。太强迫它覆盖onbackpressed函数,如

@Override
public void OnBackPressed(){
super.OnBackPressed();
finisch();
}

Finisch link stackoverflow

答案 1 :(得分:0)

如果您希望每次活动开始时都保留旧数据,则可以在活动2中使用“共享首选项”。

在设置文本时将数据保存为共享首选项,并在onCreate中检索以显示下次。

见下面的代码片段:

SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity2);

    sp = getSharedPreferences("textPref", Context.MODE_PRIVATE);
    if(sp.contains("text"))
        newText = sp.getString("text", "").concat(" and ").concat(getIntent().getExtras().getString("v"));
    else
        newText = getIntent().getExtras().getString("v");


    ll=(LinearLayout)findViewById(R.id.ll);
    ll.addView(createNewTextView(newText));
}

private View createNewTextView(String text) 
{
    // TODO Auto-generated method stub
    final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    final TextView txtview=new TextView(this);
    txtview.setLayoutParams(lp);
    txtview.setText("New text: "+text);

    Editor editor = sp.edit();
    editor.putString("text", text);
    editor.commit();

    return txtview;
}

希望这有帮助。

编辑根据OP的澄清说明:

要每次显示新的TextView,请尝试使用逗号分隔将每个值存储在共享首选项中。

将它们拆分为数组。因此,您可以显示文本数组。然后在循环中的新TextView中显示它们,并在LinearLayout中添加TextView。

段:

SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity2);

    sp = getSharedPreferences("textPref", Context.MODE_PRIVATE);
    if(sp.contains("text"))
        newText = sp.getString("text", "").concat(" and ").concat(getIntent().getExtras().getString("v"));
    else
        newText = getIntent().getExtras().getString("v");


    ll=(LinearLayout)findViewById(R.id.ll);
    createNewTextView(newText);
}

private void createNewTextView(String text) 
{
    // TODO Auto-generated method stub
    Editor editor = sp.edit();
    editor.putString("text", text);
    editor.commit();

    String[] texts = text.split(",");

    for (int i =0; i < texts.length; i++){
        final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        final TextView txtview=new TextView(this);
        txtview.setLayoutParams(lp);
        txtview.setText("New text: "+texts[i]);
        ll.addView(txtview);
    }
}

希望这很清楚。