Android中点击按钮获取string.xml中内容并弹窗提示

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述Android中点击按钮获取string.xml中内容并弹窗提示相关的知识,希望能为你提供帮助。
场景androidStudio跑起来第一个App时新手遇到的那些坑:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103797243
效果

Android中点击按钮获取string.xml中内容并弹窗提示

文章图片

 
 
Android中点击按钮获取string.xml中内容并弹窗提示

文章图片


注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现新建project后,打开布局文件activity_main.xml
添加一个Button
< Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/badao" android:id="@+id/listenerTest"/>

然后来到res下的values下的strings.xml中添加字符串资源
 
Android中点击按钮获取string.xml中内容并弹窗提示

文章图片

 
 
通过Toast的makeText进行提示,此方法的三个参数(当前activity,提示内容,提示显示的时间)
这里想引用字符串资源中的内容通过
R.string.toastMessage

就可以获取
< string name="toastMessage"> 公众号:霸道的程序猿< /string>

所对应的字符串内容。
然后来到Activity中,通过Id获取Button,然后设置button的点击事件监听器。
在OnCreate方法中
【Android中点击按钮获取string.xml中内容并弹窗提示】 
//提示按钮 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(); } });


    推荐阅读