####1、界面显示
A界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lumeng.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</RelativeLayout>
B界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lumeng.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="7"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txvd"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="2"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/edtxvd"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="7"
android:orientation="horizontal">
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/bt_cancle"
android:text="@string/cancle"
android:layout_marginLeft="180dp"
android:textSize="30dp"
android:onClick="onCancel"/>
<Button
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/bt_save"
android:text="@string/save"
android:textSize="30dp"
android:onClick="onSave"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
####2、功能实现
MainActivity
package com.example.lumeng.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
String[] aMemo = {"1.单击编辑备忘录","2.长按可以清除备忘录","3.","4.","5.","6."};
ListView lv; //显示备忘录的listview
ArrayAdapter<String> aa; //listview 和备忘录的桥梁
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listview);
aa=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,aMemo);
lv.setAdapter(aa); //设置listview的内容
//设置listview 被单击的监听器
lv.setOnItemClickListener(this);
//设置长按监听
lv.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
Intent it = new Intent(this,EditViewActivity.class);
it.putExtra("编号",pos+1); //附加编号
// it.putExtra("备忘",aMemo[pos]); //附加备忘录的内容
// startActivity(it);
startActivityForResult(it,pos); //启动Edit并以选项位置为标致符
}
protected void onActivityResult(int requestCode,int resultCode,Intent it){
if(requestCode == RESULT_OK){
aMemo[requestCode] =it.getStringExtra("备忘"); //使用返回的数据更新内容
Toast.makeText(this,"修改于\n"+it.getStringExtra("日期"),Toast.LENGTH_LONG).show();
aa.notifyDataSetChanged();//通知Adapter更新
}
}
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int pos, long l) {
aMemo[pos]=(pos+1)+".";//将内容清楚(只剩编号)
aa.notifyDataSetChanged();//通知ListView要更新显示的内容
return true; //返回true表示时间已处理
}
}
另一个activity
package com.example.lumeng.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static com.example.lumeng.myapplication.R.id.edtxv;
import static com.example.lumeng.myapplication.R.id.txv;
/**
* Created by lumeng on 17-12-4.
*/
public class EditViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editview);
Intent it =getIntent();//获取传入的INtent对象
// int no = it.getIntExtra("编号",0);//读出名为编号的int数据,要是没有返回0
String s= it.getStringExtra("备忘");
TextView txv= (TextView)findViewById(R.id.txvd);
EditText edtxv=(EditText) findViewById(R.id.edtxvd);
//txv.setText(no+".");//设置画面左上角的编号
txv.setText(s.substring(0,2));//将txv改在第二行声明将编号显示在画面左上角
if(s.length()>3) {
edtxv.setText(s.substring(3)); //将传过来的数据去除前三个字符,然后填入edtxv中
}
}
public void onCancel (View v){
setResult(RESULT_CANCELED);//返回取消信息
finish();//结束activity
}
public void onSave (View v){
Intent it2 =new Intent();
it2.putExtra("备忘", txv.getText() + " " + edtxv.getText());/* 附加选项编号和修改后内容 */
it2.putExtra("日期",new Data().toString()); //附加修改日期时间
setResult(RESULT_OK,it2);//返回修改成功及修改后数据
finish();//结束activity
}
}
文献参考:
android app开发入门 施威铭 编著
本人郑重声明,本博客所著文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若以上文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。