たとえば、UI要素が定義されている場合
<TextView
android:id="@+id/output"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:typeface="monospace"
android:textSize="12sp" />
次のようなもので参照できます
TextView output = (TextView) findViewById(R.id.output);
ただし、TextView が存在しない場合は、コードR.id.output
が生成されず、コンパイル時にキャッチされるため、コードをコンパイルすることさえできません。
で参照せずに要素が存在するかどうかをテストする方法はありR.id...
ますか?
実際、それが存在するかどうかを確認するためのテストだけでなく、それを参照する別の方法も必要になると思います。
多分何かのような
TextView output = null;
if((output = findViewByName("output")) != null)
{
// Do something with output
}
(もちろんfindViewByName
機能はありません。)
ありがとう