Android中點擊按鈕獲取string.xml中內容並彈窗提示

場景

AndroidStudio跑起來第一個App時新手遇到的那些坑:android

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103797243編程

效果

 

 


注:app

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。ide

實現

新建project後,打開佈局文件activity_main.xml佈局

添加一個Buttonthis

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/badao"
        android:id="@+id/listenerTest"/>

而後來到res下的values下的strings.xml中添加字符串資源spa

 

 

 

經過Toast的makeText進行提示,此方法的三個參數(當前activity,提示內容,提示顯示的時間).net

這裏想引用字符串資源中的內容經過code

R.string.toastMessage

就能夠獲取xml

<string name="toastMessage">公衆號:霸道的程序猿</string>

所對應的字符串內容。

而後來到Activity中,經過Id獲取Button,而後設置button的點擊事件監聽器。

在OnCreate方法中

 

  //提示按鈕
        Button listenerButton  =  (Button)findViewById(R.id.listenerTest);
        listenerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,R.string.toastMessage,Toast.LENGTH_LONG).show();
            }
        });
相關文章
相關標籤/搜索