2. 在 MainActivity.java 中撰寫以下程式碼
public class MainActivity extends Activity {
private Button btn1; //宣告Button
private EditText txt1; //宣告EditText
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1); //取得Button
txt1 = (EditText) findViewById(R.id.editText1); //取得EditText
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//取得EditText的輸入內容
String content = txt1.getText().toString();
//顯示在Debug Console
Log.d("debug", "button click");
//使用Toast顯示在螢幕上
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).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;
}
}
3. 完成後畫面如下
沒有留言:
張貼留言