android 弹出菜单 toast,Android学习第二天:Toast(提醒)、Menu(菜单)、Intent的显式和隐式(包括打开、适配网站,调用拨号界面等)...
1.Toast提醒
為昨天寫的按鈕程序添加一個(gè)提醒,在MainActivity中添加如下代碼:
Button bt1 = (Button) findViewById(R.id.button_1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "您點(diǎn)擊了按鈕1", Toast.LENGTH_SHORT).show();
}
});
findViewById()方法用于獲得布局文件文件中的元素,值通過屬性Id指定。(該返回值是View對象,需要轉(zhuǎn)成Button對象)
setOnClickListener()方法是用于注冊監(jiān)聽器,點(diǎn)擊執(zhí)行
makeText()方法需要三個(gè)參數(shù),第一個(gè)是context(toast要求的上下文,活動本身就是一個(gè)context),第二個(gè)是顯示的內(nèi)容,第三個(gè)是顯示的時(shí)常。
運(yùn)行結(jié)果如下:
2.Menu菜單
首先在res目錄下創(chuàng)建一個(gè)menu文件夾,并在文件內(nèi)創(chuàng)建一個(gè)xml文件,命名為main
main.xml文件中代碼如下:
每個(gè)item標(biāo)簽為定義一個(gè)菜單中的選項(xiàng)
在MainActivity中添加如下代碼,重寫onCreatOptionsMenu()方法
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
getMenuInflater()方法能夠得到MenuInflater對象,再調(diào)用inflate()方法可以給當(dāng)前活動創(chuàng)建菜單。inflate()方法需要兩個(gè)參數(shù),第一個(gè)是從哪個(gè)資源文件來創(chuàng)建菜單,第二個(gè)用于指定菜單項(xiàng)添加到哪個(gè)Menu對象中。(返回值true用于將菜單顯示出來)
運(yùn)行結(jié)果如下:
3.為Menu菜單添加監(jiān)聽器
在MainActivity中添加如下代碼,重寫onOptionsItemSelected()方法
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.add_item:
Toast.makeText(this, "您點(diǎn)擊了Add按鈕", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "您點(diǎn)擊了Remove按鈕", Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
通過item.getItemId()方法來判斷點(diǎn)擊的是哪個(gè)菜單項(xiàng)。
運(yùn)行結(jié)果如下(點(diǎn)擊Add):
4.顯式Intent
首先再創(chuàng)建一個(gè)Activity,命名為SecondActivity
public class SecondActivity extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
在layout中再創(chuàng)建一個(gè)xml,命名為activity_second
我讓這個(gè)新的活動顯示Hello World
最后到AndroidManifest中給新的活動注冊
為了能夠使用第二個(gè)活動,將MainActivity中的button的監(jiān)聽器改為:
Button bt1 = (Button) findViewById(R.id.button_1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
程序運(yùn)行結(jié)果如下(點(diǎn)擊按鈕Button 1):
Intent()方法需要兩個(gè)參數(shù),第一個(gè)參數(shù)context要求提供各一個(gè)啟動活動的上下文,第二個(gè)參數(shù)class則是指定想要啟動的活動目標(biāo)。
startActivity()方法用于啟動活動,來執(zhí)行這個(gè)Intent。
5.隱式Intent
首先在AndroidManifest中修改剛才注冊的SecondActivity:
然后修改MainActivity中的按鈕監(jiān)聽器:
Button bt1 = (Button) findViewById(R.id.button_1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("bistu.com.test.ACTION_START");
startActivity(intent);
}
});
然后運(yùn)行這個(gè)程序,跟剛才的顯式效果一樣。
會發(fā)現(xiàn)在監(jiān)聽器中并沒有指定category,實(shí)際上默認(rèn)為DEFAULT。
如果在監(jiān)聽器中加上一句“intent.addCategory("bistu.com.test.MY_CATEGORY")”,則會報(bào)錯,只需要在活動注冊中,添加“”即可。
6.更多隱式Intent用法
①.將按鈕改為點(diǎn)擊打開網(wǎng)頁,將MainActivity中的監(jiān)聽器修改為:
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
});
運(yùn)行結(jié)果如下:
(點(diǎn)擊Button后,用瀏覽器打開了百度的網(wǎng)站)
通過Uri.parse()方法將王志字符串解析成一個(gè)Uri對象,調(diào)用Insert的setData()方法將這個(gè)Uri對象傳遞進(jìn)去。
②.在①的基礎(chǔ)上,讓這個(gè)程序適配網(wǎng)頁
修改注冊代碼為:
點(diǎn)擊按鈕后:
可以選擇用該軟件來適配網(wǎng)頁(但是不能使用)
③.調(diào)用系統(tǒng)撥號界面
將按鈕的監(jiān)聽器改為:
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
});
點(diǎn)擊button后如下:
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的android 弹出菜单 toast,Android学习第二天:Toast(提醒)、Menu(菜单)、Intent的显式和隐式(包括打开、适配网站,调用拨号界面等)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 电视 文件目录,通用的安
- 下一篇: android m权限工具类,andro