一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

Android 按鈕示例含代碼

Android 按鈕示例

Android按鈕表示一個按鈕。愛掏網 - it200.comandroid.widget.Button是TextView類的子類,CompoundButton是Button類的子類。愛掏網 - it200.com

在Android中有不同類型的按鈕,例如RadioButton,ToggleButton,CompoundButton等。愛掏網 - it200.com

在這里,我們將創建兩個文本字段和一個按鈕用于兩個數字的總和。愛掏網 - it200.com如果用戶點擊按鈕,兩個輸入值的總和將顯示在Toast上。愛掏網 - it200.com

我們可以使用不同的方式對按鈕執行操作,例如在按鈕上調用監聽器或在Activity的xml文件中添加onClick屬性。愛掏網 - it200.com

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //code
            }
});
<Button  
        android:onClick="methodName"  
/>  

拖動組件或在activity_main.xml中編寫UI代碼

首先,從文本字段調色板中拖動兩個文本字段,并從表單小部件調色板中拖動一個按鈕,如下圖所示。愛掏網 - it200.com

生成的ui組件的代碼將如下所示:

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.sumoftwonumber.MainActivity">  

    <EditText  
        android:id="@+id/editText1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="61dp"  
        android:ems="10"  
        android:inputType="number"  
        tools:layout_editor_absoluteX="84dp"  
        tools:layout_editor_absoluteY="53dp" />  

    <EditText  
        android:id="@+id/editText2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/editText1"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="32dp"  
        android:ems="10"  
        android:inputType="number"  
        tools:layout_editor_absoluteX="84dp"  
        tools:layout_editor_absoluteY="127dp" />  

    <Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/editText2"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="109dp"  
        android:text="ADD"  
        tools:layout_editor_absoluteX="148dp"  
        tools:layout_editor_absoluteY="266dp" />  
</RelativeLayout> 

Activity類

現在編寫代碼顯示兩個數字的和。愛掏網 - it200.com

package example.javatpoint.com.sumoftwonumber;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText edittext1, edittext2;
    private Button buttonSum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();
    }

    public void addListenerOnButton() {
        edittext1 = (EditText) findViewById(R.id.editText1);
        edittext2 = (EditText) findViewById(R.id.editText2);
        buttonSum = (Button) findViewById(R.id.button);

        buttonSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String value1=edittext1.getText().toString();
                String value2=edittext2.getText().toString();
                int a=Integer.parseInt(value1);
                int b=Integer.parseInt(value2);
                int sum=a+b;
                Toast.makeText(getApplicationContext(),String.valueOf(sum), Toast.LENGTH_LONG).show();
            }
        });
    }
}

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 毛色毛片免费看 | 国产一区二区三区视频在线观看 | 81精品国产乱码久久久久久 | 东方伊人免费在线观看 | 精品一区免费 | 国产精品精品视频一区二区三区 | 精品日韩一区 | 日日爱夜夜操 | 毛片一级电影 | 夜夜精品浪潮av一区二区三区 | 久久综合伊人 | 国产精品夜间视频香蕉 | 国产一级在线观看 | 久久久久久久夜 | 亚洲视频一区在线观看 | 欧美日韩一卡 | 国产 日韩 欧美 在线 | 免费成人av| 国产精品视频一区二区三区不卡 | 羞羞视频在线观免费观看 | 日本h片在线观看 | 国产一区二区精品在线观看 | 成人免费毛片在线观看 | 丝袜美腿一区二区三区动态图 | av免费网站在线 | 成人精品鲁一区一区二区 | 日本超碰 | 欧美日韩中文字幕在线 | 日韩欧美专区 | 成人免费小视频 | 国产精品入口麻豆www | 日韩图区 | 日本一区二区三区在线观看 | 成人欧美| 性色的免费视频 | 亚洲综合成人网 | 中文字幕国产精品 | 国产精品亚洲成在人线 | 日本免费视频在线观看 | 99热在线免费| 国产精品日韩欧美一区二区三区 |