动态改变Textview

changing Textview dinamically

我是 android 应用程序的新手。我正在按照一本书动态更改文本,但它没有做它应该做的事情。我已经研究过,但找不到问题出在哪里。该应用程序可以找到但不更改文本。 我被困住了,我真的很感激一些帮助。

Java 文件:

public class ViewDVDActivity extends  Activity {

    TextView txtTituloDVD;
    TextView txtAnyoDVD;
    TextView txtActor1;
    TextView txtActor2;
    TextView txtResumenPelicula;

    @Override
    protected void onCreate ( Bundle saveInstanceState ) {
        super.onCreate(saveInstanceState);
        // asignacion layout
        setContentView(R.layout.activity_viewdvd);

        // obtención componentes
        txtTituloDVD = (TextView) findViewById(R.id.tituloDVD);
        txtAnyoDVD = (TextView) findViewById(R.id.anyoDVD);
        txtActor1 = (TextView) findViewById(R.id.actor1);
        txtActor2 = (TextView) findViewById(R.id.actor2);
        txtResumenPelicula = (TextView) findViewById(R.id.resumenPelicula);

    }

     @Override
      protected  void  onStart () {
         super.onStart();
     }

      @Override
      protected void onResume() {
          super.onResume();

          txtTituloDVD.setText("Matrix");
          txtAnyoDVD.setText(String.format(getString(R.string.anyo_de_aparicion), 2014));
          txtActor1.setText("Nelson Harris");
          txtActor2.setText("Brian Eno");
          String resumen = "En un lugar de la mancha de cuyo nombre no me acuerdo";
          txtResumenPelicula.setText(resumen);
      }

manifest.xml 文件:

<application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.LocDVD">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".ViewDVDActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

xml 文件: activity_viewdvd.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">

     <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:text="TextView"  /> 
    <TextView
        android:id="@+id/tituloDVD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Título del dvd"
        android:layout_gravity="top|center_horizontal"
        android:layout_marginTop="16dp"
        android:textSize="22sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Año"
        android:textSize="15sp"
        android:id="@+id/anyoDVD" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="18sp"
        android:text="Actor 1"
        android:id="@+id/actor1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="18sp"
        android:text="Actor 2"
        android:id="@+id/actor2" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="18sp"
        android:text="Resumen"
        android:minLines="5"
        android:maxLines="15"
        android:id="@+id/resumenPelicula" />

</LinearLayout>

字符串值:

<resources>
    <string name="app_name">LocDVD</string>
    <string name="anyo_de_aparicion">"Año de aparición : %d"</string>
    <string name="titulo_de_la_pelicula">"Títlo de la película"</string>
    <string name="anyo_de_aparicion_etiqueta">"Año de aparición"</string>
    <string name="actores">"Actores"</string>
    <string name="resumen">"Resumen"</string>
</resources>

试试这个

    public class ViewDVDActivity extends  Activity {
    
        TextView txtTituloDVD;
        TextView txtAnyoDVD;
        TextView txtActor1;
        TextView txtActor2;
        TextView txtResumenPelicula;
    
        @Override
        protected void onCreate ( Bundle saveInstanceState ) {
            super.onCreate(saveInstanceState);
            // asignacion layout
            setContentView(R.layout.activity_viewdvd);
    
            // obtención componentes
            txtTituloDVD = (TextView) findViewById(R.id.tituloDVD);
            txtAnyoDVD = (TextView) findViewById(R.id.anyoDVD);
            txtActor1 = (TextView) findViewById(R.id.actor1);
            txtActor2 = (TextView) findViewById(R.id.actor2);
            txtResumenPelicula = (TextView) findViewById(R.id.resumenPelicula);

txtTituloDVD.setText("Matrix");
              txtAnyoDVD.setText(String.format(getString(R.string.anyo_de_aparicion), 2014));
              txtActor1.setText("Nelson Harris");
              txtActor2.setText("Brian Eno");
              String resumen = "En un lugar de la mancha de cuyo nombre no me acuerdo";
              txtResumenPelicula.setText(resumen);
    
        }
    
         @Override
          protected  void  onStart () {
             super.onStart();
         }
    
          @Override
          protected void onResume() {
              super.onResume();
    
              txtTituloDVD.setText("Matrix");
              txtAnyoDVD.setText(String.format(getString(R.string.anyo_de_aparicion), 2014));
              txtActor1.setText("Nelson Harris");
              txtActor2.setText("Brian Eno");
              String resumen = "En un lugar de la mancha de cuyo nombre no me acuerdo";
              txtResumenPelicula.setText(resumen);
          }

我觉得你导航错了activity

<application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.LocDVD">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".ViewDVDActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

因为你有两个午餐盒activity

如果你想先显示ViewDVDActivity

<application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.LocDVD">
            <activity android:name=".MainActivity">
               
            </activity>
            <activity android:name=".ViewDVDActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>