-1
我正在嘗試使按鈕更改爲其他活動,並同時用用戶輸入的文本創建新的textView。我想通過一項活動如何做到這一點(我從中得到了這個結果:LINK)。然而,我想要做同樣的事情,除了我想讓按鈕帶我到另一個活動並在那裏創建一個textView。如何使按鈕更改活動並在android studio中創建新的textview?
這一點是要對賬戶進行概述。您進入「創建帳戶頁面」,創建一個帳戶,並在總覽中顯示該帳戶。
這是我到目前爲止有:
第一類:
public class MainActivity extends AppCompatActivity {
private EditText mEditText;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(getApplicationContext());
textView.setText("New text");
}
private View.OnClickListener onClick() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent buttonIntent = new Intent(MainActivity.this,
SecondLayout.class);
MainActivity.this.startActivity(buttonIntent);
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LinearLayout.LayoutParams lparams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}
}
第二類:
public class SecondLayout extends AppCompatActivity {
public static LinearLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
final Button addAccountButton = (Button)
findViewById(R.id.secondButton);
addAccountButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent buttonIntent = new Intent(SecondLayout.this,
MainActivity.class);
SecondLayout.this.startActivity(buttonIntent);
}
});
}
}
XML第一活動:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/firstlayout">
<EditText
android:id="@+id/firstText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
</LinearLayout>
XML的第二項活動:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/firstlayout">
<EditText
android:id="@+id/firstText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
</LinearLayout>
我對android studio和java很陌生,所以可能會出現一些我看不見的明顯錯誤。