如果选择按钮来更改文本大小
if the button is selected to change the text size
这一定是公平的,在选择一个按钮以更改文本大小时?
我尝试在 onCreate if 中添加但不能正常工作,是 cheek 只有一次启动应用程序。该功能不会在下次保持激活状态。
谢谢
按钮
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/inbox_string"
android:id="@+id/button"
android:textColor="@drawable/text_button_culoare"
android:drawableLeft="@drawable/ic_view_list_white_24dp"
android:background="@android:color/transparent"
style="?android:attr/borderlessButtonStyle"
android:layout_centerHorizontal="true"
android:gravity="left|center_vertical"
android:layout_gravity="center_horizontal"
android:focusable="true"
android:enabled="true"
android:clickable="true"
android:contextClickable="true"
android:elegantTextHeight="true"
android:layout_marginTop="16dp" />
Activity:
public class Work_screen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_screen);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.button) {
}
}
};
findViewById(R.id.button).setOnClickListener(clickListener);
final Button button = (Button) findViewById(R.id.button);
if (button.isSelected()) {
Context context = getApplicationContext();
CharSequence text = "selected";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
button.setTextSize(22);
} else {
Context context = getApplicationContext();
CharSequence text = "not";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
您应该在 onClick 方法中放置一些代码,因为当您单击该按钮时会调用此方法。您的代码只被第一次调用的原因是 ONCREATE 方法在您的应用程序创建时只被调用一次。
只需将您的 if else 语句放入您的 onClick 方法中,它就会起作用。
您应该将按钮变量创建为 class 变量,以便在您的侦听器中访问它,即使它不是您的 onCreate 方法的一部分。
这一定是公平的,在选择一个按钮以更改文本大小时? 我尝试在 onCreate if 中添加但不能正常工作,是 cheek 只有一次启动应用程序。该功能不会在下次保持激活状态。 谢谢
按钮
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/inbox_string"
android:id="@+id/button"
android:textColor="@drawable/text_button_culoare"
android:drawableLeft="@drawable/ic_view_list_white_24dp"
android:background="@android:color/transparent"
style="?android:attr/borderlessButtonStyle"
android:layout_centerHorizontal="true"
android:gravity="left|center_vertical"
android:layout_gravity="center_horizontal"
android:focusable="true"
android:enabled="true"
android:clickable="true"
android:contextClickable="true"
android:elegantTextHeight="true"
android:layout_marginTop="16dp" />
Activity:
public class Work_screen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_screen);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.button) {
}
}
};
findViewById(R.id.button).setOnClickListener(clickListener);
final Button button = (Button) findViewById(R.id.button);
if (button.isSelected()) {
Context context = getApplicationContext();
CharSequence text = "selected";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
button.setTextSize(22);
} else {
Context context = getApplicationContext();
CharSequence text = "not";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
您应该在 onClick 方法中放置一些代码,因为当您单击该按钮时会调用此方法。您的代码只被第一次调用的原因是 ONCREATE 方法在您的应用程序创建时只被调用一次。
只需将您的 if else 语句放入您的 onClick 方法中,它就会起作用。 您应该将按钮变量创建为 class 变量,以便在您的侦听器中访问它,即使它不是您的 onCreate 方法的一部分。