断断续续,没有办法,又重头学习下基础知识。
- 很简单的一个功能,利用intent传递数据
- 菜单功能
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bbw.androidtrainingcourse01.MainActivity">
<EditText
android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/edit_message"/>
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
传输数据
package com.example.bbw.androidtrainingcourse01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String MESSAGE = "com.example.bbw,androidtrainingcourse01.MESSAGE";
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent = new Intent(this,DisplayMessage.class);
editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(MESSAGE,message);//以键值对的形式传输数据
startActivity(intent);
}
}
接收数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MESSAGE);
displayMessage = (TextView) findViewById(R.id.display_Message);
displayMessage.setText(message);
}
菜单功能
onCreateOptionsmenu()创建Menu菜单的项目
onOptionsItemSelected()处理菜单被选择运行后的事件处理
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_item"
android:title="Add"/>
<item
android:id="@+id/remove_item"
android:title="Remove"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_item:
Toast.makeText(this,"you click add item",Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this,"you click remove item",Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}