在机器人设计的国际象棋国际象棋、机器人

2023-09-04 07:10:56 作者:我是你的彼得潘

我通过使用按钮在表中的布局设计用于棋局的用户界面。对于棋子我把ASCII值,并印在按钮上的字符串值。但我不知道怎么弄的值单击按钮时。这是我第一次编程为Android。

 <按钮机器人:ID =@ + ID / A8机器人:layout_width =36dp机器人:layout_height =36dp机器人:背景=@色/白机器人:提示=@字符串/ b_rook机器人:TEXTSIZE =25sp/>
 

解决方案

如果你只是想按钮的文本值,请将以下 OnClickListener 来所有的按钮

  OnClickListener myButtonClickListener =新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            串buttonText =((按钮)ⅴ).getText();
                            //做你想做的与buttonText

        }
    };
 
群星汇聚 大战将至 第一届 机器人谷杯 中国城市国际象棋联赛正式启动

要为它指定为按钮实例则myButton:

  myButton.setOnClickListener(myButtonClickListener);
 

I designed a user interface for a chess game by using buttons in a table layout. For the chess pieces I took the ASCII values and printed on the button as a string value. But I don't know how get that value when a button is clicked. This is the first time I am programming for Android.

       <Button android:id="@+id/a8" android:layout_width="36dp" android:layout_height="36dp" android:background="@color/white" android:hint="@string/b_rook" android:textSize="25sp"/>

解决方案

If you just want the text value of the button, attach the following OnClickListener to all of your buttons:

OnClickListener myButtonClickListener = new OnClickListener() { 
        @Override
        public void onClick(View v) {
            String buttonText = ((Button) v).getText();
                            //Do whatever you want with buttonText

        }
    };

To assign it for the Button instance "myButton":

myButton.setOnClickListener(myButtonClickListener );