一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久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%準確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 中文在线一区二区 | 丁香婷婷在线视频 | 国产探花在线精品一区二区 | 国产日韩一区二区三区 | 一级欧美一级日韩片 | 男人亚洲天堂 | 一级久久久久久 | 日本精品视频一区二区 | 99久久免费精品 | 欧美一级久久 | 国产电影一区二区在线观看 | 1000部精品久久久久久久久 | 天天操夜夜爽 | 中文字幕免费视频 | 在线免费激情视频 | 成人一区二区电影 | 精品一区久久 | 国产精品久久毛片av大全日韩 | 欧美不卡一区二区三区 | 天天综合国产 | 亚洲精品白浆高清久久久久久 | 蜜臀网站| 中文在线一区二区 | 亚洲欧美国产精品一区二区 | 色爱av| 亚洲91视频 | 色婷婷综合成人av | 日韩av网址在线观看 | 国产精品婷婷 | 日韩精品视频在线免费观看 | 亚洲高清电影 | 国产专区免费 | 亚洲一区在线观看视频 | 国产精品久久 | 99久久精品免费看国产四区 | www一级片| av黄色片在线观看 | 岛国av在线免费观看 | 国产在线一区观看 | 中文字幕在线观看一区二区 | 成人免费片 |