大家好,我是 android 开发的新手,事实上它是我的第一个应用程序。我想知道
<button
android:text="1" />
在上面的标签中,文本是按钮的值吗?如果是,那么我如何获取该值或将其存储在变量中。如果没有,那么如何在android中的任何按钮后面定义一个值?
大家好,我是 android 开发的新手,事实上它是我的第一个应用程序。我想知道
<button
android:text="1" />
在上面的标签中,文本是按钮的值吗?如果是,那么我如何获取该值或将其存储在变量中。如果没有,那么如何在android中的任何按钮后面定义一个值?
您首先需要给按钮一个 id,如下所示:
<Button
android:id="@+id/buttonId"
android:text="1"
/>
然后在您的代码中执行以下操作:
Button b = (Button)findViewById(R.id.buttonId);
b.getText(); // returns the value of your text.
是的,它是 Button 的值,使用以下代码获取 Button 的文本。
Button b = (Button)findViewById(R.id.button1);
String buttonText = b.getText().toString();
函数调用
b.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
function();
}
});
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ButtonText" />
...
@Override
public void onCreate(Bundle savedInstanceState) {
Button btn = (Button) findViewById(R.id.button1);
String text = btn.getText().toString();
}