나는 스스로 안드로이드를 가르치려고 노력하고 있는데 완전히 갇혀있다. 기본 텍스트가 "button"인 9 개의 버튼 배열과 기본 텍스트가 "Hello World!"인 TextView가 있습니다.
I can get the TextView's text to change to Lorem Ipsum,
but I can't get the button array text to change to "Btn something"
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
rv = new RemoteViews(getPackageName(), R.layout.activity_main);
int i = 0;
bArr[i++] = (Button) findViewById(R.id.button1);
bArr[i++] = (Button) findViewById(R.id.button2);
bArr[i++] = (Button) findViewById(R.id.button3);
bArr[i++] = (Button) findViewById(R.id.button4);
bArr[i++] = (Button) findViewById(R.id.button5);
bArr[i++] = (Button) findViewById(R.id.button6);
bArr[i++] = (Button) findViewById(R.id.button7);
bArr[i++] = (Button) findViewById(R.id.button8);
bArr[i] = (Button) findViewById(R.id.button9);
for(i = 0; i < 0; i++) {
bArr[i].setText("Btn " + i); //Doesn't Work
rv.setTextViewText(bArr[i].getId(), "Btn " + i); // Doesn't work
bArr[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Btn pressed", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
((TextView)findViewById(R.id.text1)).setText("Lorem Ipsum"); // Works
}
그리고 XML 파일 :
<android.support.constraint.ConstraintLayout
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"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="edu.csub.cs.cenri3390.MainActivity"
tools:showIn="@layout/activity_main"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/button4"
android:layout_marginTop="11dp"
app:layout_constraintTop_toBottomOf="@+id/button4"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/button4" />
<!-- 8 more buttons.../>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="91dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
버튼이 고장 났지만 상관 없으면 좋겠어요. 어쨌든 올바른 위치에 배치됩니다.
for 루프가 여기에서 부적절하게 인스턴스화되어 실행되지 않습니다. 물론 결코 사실이 아닌 i=0
만큼 오래 실행되도록 루프를 설정 하고 지시했습니다 i<0
.
for(i = 0; i < 0; i++) {
bArr[i].setText("Btn " + i); //Doesn't Work
rv.setTextViewText(bArr[i].getId(), "Btn " + i); // Doesn't work
bArr[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Btn pressed", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
for(i=0;i<9;i++)
항상 9 개의 버튼이 있다는 것을 알고 있다면 그렇게 할 수 있습니다 .
또한 사용하는 버튼을 반복 하고 개별적으로 액세스하는 데 layout.getChildCount()
사용할 수있는 팁 layout.getChildAt(i)
입니다. 그래서하는 대신
rv = new RemoteViews(getPackageName(), R.layout.activity_main);
int i = 0;
bArr[i++] = (Button) findViewById(R.id.button1);
...
bArr[i] = (Button) findViewById(R.id.button9);
당신은 할 수 있습니다
for(i=0;i<layout.getChildCount();i++) {
// Do something
layout.getChildAt(i).setText('something');
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다