Java 如何创建更改背景和字体文本的按钮?
大家好,我对Android编程完全陌生,我想学习的一件事是如何基于按键触发事件。到目前为止,我只创建了一个按钮,但我不知道如何创建事件,更重要的是,我必须修改哪些文件 activity_main.xml:Java 如何创建更改背景和字体文本的按钮?,java,android,xml,button,fonts,Java,Android,Xml,Button,Fonts,大家好,我对Android编程完全陌生,我想学习的一件事是如何基于按键触发事件。到目前为止,我只创建了一个按钮,但我不知道如何创建事件,更重要的是,我必须修改哪些文件 activity_main.xml: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button_id" android:tex
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_id"
android:text="@string/font_color"
android:onClick="toggleChange"/>
我不确定该在函数中放置什么,也不完全确定只需要java。我是否需要更改我不知道的其他XML文件?谢谢你的帮助
public boolean onCreateOptionsMenu(Menu menu) {
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
// Use attributes of View or cast to Button
// to change background / text
//v.setText ("Hello Blu");
}
});
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
可能更常见的方法是
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
// Use attributes of View or cast to Button
// to change background / text
v.setText ("Hello Blu");
}
});
在xml中实现TextView,并在java中进行设置。 XML:
你应该在youtube上观看这个安卓系列,你可以从那里学到安卓开发的基本知识:我已经安装了Eclipse,PauloI的意思是这个系列不是第一个视频。通常是用onCreate方法。也许现在最安全。我遇到了一些错误:Button无法解析为类型,Button\u id无法解析或不是字段,类型视图的方法setText(string)未定义尝试导入它导入android.view.view之后,两个错误消失了,剩下的是:Button\u id无法解析或不是字段,对于类型viewYes,方法setText(string)是未定义的。是的,您没有在xml中为按钮指定ID,是吗。我建议你回到最基本的方面,也许可以看看保罗发给你的youtube链接。
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
// Use attributes of View or cast to Button
// to change background / text
v.setText ("Hello Blu");
}
});
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
private TextView mText;
mText = (TextView) findViewById(R.id.text);
Button mButton
= (Button) mRootView.findViewById(R.id. button_id);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mText.setText("whatever you like");
}
});