Get the text inside a button
<Button android:id="@+id/button1" android:text="blah blah">开发者_如何学编程
How do I get the text inside android:text
?
Button btn = (Button)findViewById(R.id.button1);
String buttontext = btn.getText().toString();
Hope this will help you.
you get text by.
Button button = (Button) findViewById(R.id.button1);
String yourText = (String)button.getText();
TextView button1 = (TextView)findViewById(R.id.button1);
String text = button1.getText().toString();
Button mButton;
mButton=(Button)findViewById(R.id.button1);
String buttontext= mButton.getText().toString();
Button tv = (Button) findViewById(R.id.button1);
String buttonName = tv.getText().toString();
Edit according to the comment below, thanks :).
Try to keep all your strings in the strings.xml (rather than hard-coding them everywhere)
<Button android:id="@+id/button1" android:text="@string/button1_label">
In the strings.xml:
<string name="button1_label">blah blah</string>
Then you can easily get the text using:
Context.getString(R.string.button1_label)
You can find out more here.
精彩评论