은 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>
뜻은 .. 먼저 활동 1에 "data1"을 입력하고 활동 2로 이동하십시오. "data1"이 표시되어야합니다. 다시 누를거야. 활동 2에 "data 2"를 입력하고 활동 2로 이동하십시오. 이제 "data 1 data 2"가 표시되어야합니까? –
먼저 활동 1에 "data1"을 입력하고 활동 2로 이동하십시오."data1"이 표시되어야합니다. 뒤로 누르십시오. 활동 1에 "data 2"를 입력하고 활동 2로 이동하면 이제 "data1 and data2"가 표시됩니다. – Subi
매번 활동이 만들어지기 때문에 어렵습니다. 한 가지를 할 수 있습니다. 매번 Shared Preference에 쉼표로 구분 된 문자열을 입력하십시오. 그리고 매번 textviews에 루프로 표시하십시오. –