如何根据按下的按钮更改编辑文本框的实际大小?
How do i change the actual size of the edit text box depending on what button is pressed?
我正在尝试为木制标志制作一个自定义应用程序。当用户点击 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>
您的应用程序崩溃可能是因为 NullPointerException。这是因为您还没有在 onCreate 方法中初始化您的 Wood 变量。
你应该做的是:
Wood = (EditText) findViewById(R.id.Wood);
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);
当您的应用程序崩溃时,logcat Studio 中存在 logcat 错误堆栈跟踪。堆栈跟踪显示导致崩溃的异常。您可以通过查看 logcat 日志轻松解决问题。
您从未初始化 EditText
,这导致 NullPointerException
添加这个
Wood = (EditText) findViewById(R.id.Wood);
在 onCreate
方法中。
更新: 这是您可以在单击
按钮时更改 EditText
的高度和宽度的方法
final LayoutParams lparams = new LayoutParams(150,50); // First param is Width and second is height
Wood.setLayoutParams(lparams);
我正在尝试为木制标志制作一个自定义应用程序。当用户点击 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>
您的应用程序崩溃可能是因为 NullPointerException。这是因为您还没有在 onCreate 方法中初始化您的 Wood 变量。
你应该做的是:
Wood = (EditText) findViewById(R.id.Wood);
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);
当您的应用程序崩溃时,logcat Studio 中存在 logcat 错误堆栈跟踪。堆栈跟踪显示导致崩溃的异常。您可以通过查看 logcat 日志轻松解决问题。
您从未初始化 EditText
,这导致 NullPointerException
添加这个
Wood = (EditText) findViewById(R.id.Wood);
在 onCreate
方法中。
更新: 这是您可以在单击
按钮时更改EditText
的高度和宽度的方法
final LayoutParams lparams = new LayoutParams(150,50); // First param is Width and second is height
Wood.setLayoutParams(lparams);