Toast用于向用户显示一些帮助/提示。
R.java中某部分代码如下
01.public static final class string {
02. public static final int action_settings=0x7f050001;
03. public static final int app_name=0x7f050000;
04. public static final int hello_world=0x7f050002;
05.}
String.xml中部分代码如下
<string name="app_name">EditText</string>
02. <string name="action_settings">Settings</string>
03. <string name="hello_world">Hello world!</string>
ManActivity.java中代码如下
public class MainActivity extends Activity {
private Button btn1 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
EditText editText1 =(EditText)findViewById(R.id.editText1);
String str1=editText1.getText().toString();
System.out.println(str1);
Toast.makeText(MainActivity.this, str1,Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "@String/hello_world",Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, 0x7f050002,Toast.LENGTH_LONG).show(); }
} );
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
See It
1. Toast.makeText(.........).show()
如果不加show()会无法显示信息
2. Toast有两种方法,参数不同。
Toast.makeText(context, text, duration)
Toast.makeText(context, resId, duration)
text表示字符串resId表示整形变量,表示R.java中元素唯一标识符,来显示元素。