문자열의 배열로부터 적당한 요소를 얻을 ViewFactory의 안전 incremeneting 카운터를 구현 fix
가진 단순한 예이다.
홈페이지 :
public class MainActivity extends Activity implements OnClickListener, ViewFactory {
private TextSwitcher mSwitcher;
private int counter = 0;
private String[] words = new String[]{"one","two","three"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwitcher = (TextSwitcher) findViewById(R.id.textswitcher);
mSwitcher.setFactory(this);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
Button nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(this);
updateCounter();
}
public void onClick(View v) {
counter++;
updateCounter();
}
private void updateCounter() {
int index = counter % words.length;
mSwitcher.setText(String.valueOf(words[index]));
}
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
return t;
}
}
와 XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextSwitcher
android:id="@+id/textswitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
당신이 요소의 배열에 범위를 벗어난와 요소를 호출 할 경우 잘 작동하지 않습니다 표시 ...에주의하시기 바랍니다 그것.
무엇이 prog [x]입니까? – deadfish
배열 : prog [0] = "one", prog [1] = "two" –
x 색인이 범위를 벗어나는 경우 어떻게됩니까? – deadfish