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

Android 按鈕示例含代碼

Android 按鈕示例

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

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

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

我們可以使用不同的方式對按鈕執(zhí)行操作,例如在按鈕上調(diào)用監(jiān)聽器或在Activity的xml文件中添加onClick屬性。愛掏網(wǎng) - it200.com

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

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

首先,從文本字段調(diào)色板中拖動兩個文本字段,并從表單小部件調(diào)色板中拖動一個按鈕,如下圖所示。愛掏網(wǎng) - 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類

現(xiàn)在編寫代碼顯示兩個數(shù)字的和。愛掏網(wǎng) - 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();
            }
        });
    }
}

輸出:

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

返回頂部

主站蜘蛛池模板: 亚洲欧洲日本国产 | 男人天堂网址 | 亚洲成在线观看 | 欧美一区二区久久 | 国产精品美女久久久久久久网站 | 亚洲 中文 欧美 日韩 在线观看 | 日本不卡免费新一二三区 | 亚洲精品www久久久 www.蜜桃av | 欧美二区在线 | 91久久久久 | 在线免费观看毛片 | 免费精品视频在线观看 | 欧美一级毛片免费观看 | 日韩色综合 | 中文字幕精品一区二区三区精品 | 成人 在线 | 在线免费看黄 | 欧美国产精品一区二区 | 免费 视频 1级 | 精彩视频一区二区三区 | 一区二区视频在线观看 | 欧美在线资源 | 成人小视频在线免费观看 | 久久久久久亚洲欧洲 | 亚洲在线免费观看 | 亚洲精品白浆高清久久久久久 | 精品一区电影 | 国产精品自拍啪啪 | 国产成人精品一区二区三区在线 | 9191在线播放 | 91精品国产综合久久福利软件 | 亚洲精品电影网在线观看 | 91色在线| 欧美中文字幕 | 成人片免费看 | 免费午夜剧场 | 天天视频一区二区三区 | 久久精品黄色 | 精品中文字幕一区二区三区 | 91久久精品国产91久久 | 97碰碰碰 |