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 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 国产在线视频一区二区三区98| 综合图区亚洲欧美另类图片| 欧美午夜片欧美片在线观看 | 日本高清在线免费| 性一交一乱一视频免费看| 国产v亚洲v天堂无码网站| 久久er99热精品一区二区| 足恋玩丝袜脚视频免费网站| 日本理论片和搜子同居的日子演员| 国产成人精品综合在线观看| 么公的又大又深又硬视频| 韩国欧洲一级毛片免费| 日本一区免费观看| 四虎精品影院在线观看视频| 东京一本一道一二三区 | 一个人看的www高清频道免费| 精品久久久久久久久久中文字幕 | 中文天堂最新版www在线观看| 久久丝袜精品综合网站| 色综合久久久无码中文字幕| 搞黄网站免费看| 免费网站看v片在线18禁无码| www性久久久com| 正在播放pppd| 把极品白丝班长啪到腿软| 又大又湿又紧又爽a视频| 免费a级毛片无码鲁大师| 亚洲av无码成人精品区狼人影院| 激情综合网五月| 日本在线高清视频| 国产精品无码无卡无需播放器| 亚洲国产综合人成综合网站00| www.com.av| 欧美日韩中文视频| 国产欧美日韩另类va在线 | 欧美性xxxxx极品| 大臿蕉香蕉大视频成人| 噗呲噗呲好爽轻点| free性熟女妓女tube| 精品无码av无码免费专区| 在线观看无码的免费网站|