-1

こんにちは、私はアンドロイド開発に不慣れで、実際には私の最初のアプリケーションです。私は知りたいです

<button
 android:text="1" />

上記のタグで、テキストはボタンの値ですか? はいの場合、この値を取得する方法、または変数に格納する方法を教えてください。そうでない場合、アンドロイドのボタンの背後にある値を定義する方法は?

4

3 に答える 3

4

まず、次のようにボタンにIDを指定する必要があります。

<Button 
android:id="@+id/buttonId"
android:text="1"
 />

そして、あなたのコードで次のようなことをします:

Button b = (Button)findViewById(R.id.buttonId);
b.getText(); // returns the value of your text. 
于 2012-05-01T13:15:54.507 に答える
4

はい、その値は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();
    }
});
于 2012-05-01T13:16:47.020 に答える
2
<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();

}
于 2012-05-01T13:16:57.287 に答える