我正在嘗試爲木製標牌製作一個定製應用程序。當用戶點擊10 x10大小的標誌時,我想讓編輯文本框更改大小以顯示他們已完成的操作。這可以在android studio上完成嗎? 這是我到目前爲止的代碼,我認爲我所做的是改變了文本的大小,但當按鈕被點擊時它崩潰,所以我不知道。 Java文件:如何根據按下哪個按鈕來更改編輯文本框的實際大小?
public class Letters extends AppCompatActivity {
int txtSize = 14;
EditText Wood;
Button bSize, bSize1, bSize2, bSize3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_letters);
Button bSize = (Button) findViewById(R.id.bSize);
Button bSize1 = (Button) findViewById(R.id.bSize1);
Button bSize2 = (Button) findViewById(R.id.bSize2);
Button bSize3 = (Button) findViewById(R.id.bSize3);
bSize.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 15) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 16) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 17) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 18) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
}
}
XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="60dp"
android:layout_width="match_parent"
android:weightSum="1">
<Button
android:id="@+id/bSize"
android:text="15 x 10 cm "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bSize" />
<Button
android:id="@+id/bSize1"
android:text="20 x 15 cm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bSize1" />
<Button
android:id="@+id/bSize2"
android:text="30 x 7 cm"
android:layout_width="103dp"
android:layout_height="61dp"
android:onClick="bSize2" />
<Button
android:id="@+id/bSize3"
android:text="30 x 20 cm"
android:layout_width="0dp"
android:layout_marginBottom="20dp"
android:layout_height="160dp"
android:onClick="bSize3"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/Wood"
android:textSize="40dp"
android:layout_alignTop="@+id/button3"
android:layout_centerHorizontal="true"
android:background="#795548"
android:hint="Choose Letters.."/>
</LinearLayout>
順便說一句,你應該遵循變量的標準命名約定,所以變量「木」應該開始用小寫字母「木頭」。 – chRyNaN