Здравствуйте,
Я новичок.
Начал читать и очень понравилась подача материала. Спасибо большое автору.
Всегда хочется применить, то, что узнал, сразу на практике, но не всегда получается.
Надеюсь на помощь мэтров сейчас и в дальнейшем. Когда-нибудь смогу помогать сам.
Суть вопросов (вынужден выложить коды, так как, еще не совсем владею лингвистикой Android):
1. Создал новую Activity посредством Intent, но в полученной Activity не отображаются элементы, указанные в secondactivity.xml, за исключением <TextView>. Подозреваю, что неправильно прописал manifest. К сожалению, в NetBeans нет возможности автоматически настраивать manifest, поэтому пришлось искать информацию и писать manifest для новой Activity самому.
2. Intent передал объект String и под него создана переменная textView. Возможно ли включить переменную textView как параметр в secondactivity.xml с тем, чтобы при вызове setContentView(R.layout.secondactivity) отображался полученный объект и все указанные в secondactivity.xml элементы (в данном случае Button).
Получается, что когда пишу setContentView(R.layout.secondactivity) показывает текст из android:text="@string/results" без кнопки Button, а тем более переданного текста (что и понятно, так как при setContentView(textView) показывает текст, полученный из первой Activity).
Буду признателен за ответ, очень поможет в дальнейшем понимании материала.
Здесь исходники:
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText=(EditText) findViewById(R.id.edit_message);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
String message = editText.getText().toString();
intent.putExtra("mymessage", message);
startActivity (intent);
}
}
public class DisplayMessageActivity extends Activity {
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String arrivedMessage = getIntent().getStringExtra("mymessage");
// Create the text view
textView = new TextView (this);
textView.setTextSize(40);
textView.setText(arrivedMessage);
setContentView(R.layout.secondactivity);
}
}
secondactivity.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="
http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/results"
android:textColor="@color/comments_color">
</TextView>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:text="OK">
</Button>
</RelativeLayout>
Manifest
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
package="example.AndroidApplication1"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<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=".DisplayMessageActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
</application>
</manifest>