私はtextViews
を持っていますactivity
を持っていますが、textViews
にはsetText()
メソッドが適用されています。ここでの問題は、最初textView
は2131099682 text
と第二を示していることアンドロイドのsetText()にはゴミ値が表示されます
public class MainActivity extends AppCompatActivity {
TextView text1,text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.text1);
text2=(TextView)findViewById(R.id.text2);
text1.setText(R.string.display+" text");
text2.setText("Display"+" text");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shaloin.settext.MainActivity">
<TextView
android:id="@+id/text1"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:textSize="20sp"
android:layout_height="wrap_content"
android:textColor="#000000"/>
<TextView
android:id="@+id/text2"
android:layout_marginTop="30dp"
android:layout_below="@+id/text1"
android:layout_width="match_parent"
android:textSize="20sp"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</RelativeLayout>
のstrings.xml
<string name="display">Display : </string>
:コードを以下に示します。 textView
はDisplay text
を示します。なぜこれが起こっているのか理解できません。
また、text1.setText(R.string.display+" text");
を text1.setText(R.string.display);
に変更すると、通常の出力はDisplay
になります。誰も私の疑いを明確にしてください。出力を示す画像を添付しました。ありがとう:)
なぜ 'text1.setText(R.string.display);'はガベージ値を表示しませんか? –
これは、setTextメソッドがIDまたは文字列のいずれかを取るためです。 IDだけを渡すと、その文字列が検索されます。あなたがしていたのは 'R.string.display +" text "'だったので、関数に1232123を渡すのではなく、文字列である "1232123 text"を渡し、関数はそのまま文字列を出力します。 – Aenadon
説明ありがとうございました:) –