User interface 基于组件的Android
我需要创建一个可以管理备忘录的程序User interface 基于组件的Android,user-interface,User Interface,我需要创建一个可以管理备忘录的程序 创建“MyMemoInfoList”程序 通过MyMeMoEditor程序以列表形式打印内容。(备忘录、标题、日期) 当您在选择项目后单击编辑按钮时,它会在MyMemoEditor屏幕上显示详细内容,编辑内容后,更新的内容将反映在列表上 当您选择一个项目并单击“删除”按钮时,它将从列表中删除该项目 如果您按下add按钮,它将弹出MyMemoEditor屏幕,您可以通过按下save按钮输入内容并保存。而且,它应该出现在列表中 创建MyMemoEditor程序
- 通过MyMeMoEditor程序以列表形式打印内容。(备忘录、标题、日期)
- 当您在选择项目后单击编辑按钮时,它会在MyMemoEditor屏幕上显示详细内容,编辑内容后,更新的内容将反映在列表上
- 当您选择一个项目并单击“删除”按钮时,它将从列表中删除该项目
- 如果您按下add按钮,它将弹出MyMemoEditor屏幕,您可以通过按下save按钮输入内容并保存。而且,它应该出现在列表中
- 当您在列表的主屏幕上注册新备忘录时,新备忘录将被保存
- 当您执行编辑菜单时,它将更新原始备忘录
- MyMemoMain类的事件应以匿名嵌套类形式实现
- MyMemoInfoList类的事件应该通过继承侦听器来实现
提前感谢。首先,您的MyMemoInfoList.xml将如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyMemoInfoList"
android:orientation="vertical"
>
<ListView
android:id="@+id/infolist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
</LinearLayout>
==================================================================================
现在,让我们转到MyMemoEditor(活动):
===================================================================
现在让我们看看客户机(类):
========================================================================
首先,让我们编辑xml文件
=========================================================================
以下是主页的主xml
(请确保根据给出的问题更改布局格式)
==================================================================================
我们将创建的下一个类是NoteManager(类):
====================================================================================
最后,我们创建客户机/服务器:
首先,让我们创建一个服务器(其中有一个新的java项目+类):
另外,不要忘记从清单文件更改您的权限设置
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class NoteServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8888);
System.out.println("waiting....");
while(true){
Socket socket = server.accept();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
while(true){
try{
// Note note = in.readObject();
String note = (String)in.readObject(); //use this if you don't remember.
System.out.println(note);
}catch(EOFException e){
break;
}
}
}
}
}
=============================================================================
最后,我们将创建新的客户端类:
package com.example.mymemomain2;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class MemoClient extends Thread {
ArrayList list = null;
public MemoClient(ArrayList list){
this.list = list;
}
@Override
public void run() {
super.run();
ObjectOutputStream objOut = null;
Socket socket = null;
try {
socket = new Socket("70.12.109.92", 7777);
OutputStream out = socket.getOutputStream();
objOut = new ObjectOutputStream(out);
for(int i = 0; i < list.size(); i++){
objOut.writeObject(list.get(i));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objOut.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.example.notepad;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
public class NoteClient extends Thread {
ArrayList<Note> list = null;
public NoteClient(ArrayList<Note> list){
this.list = list;
}
@Override
public void run() {
super.run();
Socket socket = null;
ObjectOutputStream out = null;
try{
socket = new Socket("70.12.109.92", 8888);
out = new ObjectOutputStream(socket.getOutputStream());
for (int i = 0; i < list.size(); i++) {
Note note = list.get(i);
// out.writeObject(note);
out.writeObject(note.toString()); //if used toString in Server
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.example.notepad;
导入java.io.IOException;
导入java.io.ObjectOutputStream;
导入java.net.Socket;
导入java.util.ArrayList;
公共类NoteClient扩展线程{
ArrayList list=null;
公共NoteClient(ArrayList列表){
this.list=列表;
}
@凌驾
公开募捐{
super.run();
套接字=空;
ObjectOutputStream out=null;
试一试{
插座=新插座(“70.12.109.92”,8888);
out=newObjectOutputStream(socket.getOutputStream());
对于(int i=0;i
package com.example.mymemomain2;
import java.io.Serializable;
public class MemoItem implements Serializable {
private int memoId;
private String title;
private String content;
private String date;
public MemoItem(){
}
public MemoItem(int memoId, String title, String content, String date) {
super();
this.setMemoId(memoId);
this.setTitle(title);
this.setContent(content);
this.setDate(date);
}
public int getMemoId() {
return memoId;
}
public void setMemoId(int memoId) {
this.memoId = memoId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "ID:" + getMemoId() + ","
+ getTitle() + ":" +getDate();
}
}
package com.example.mymemomain2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyMemoEditor extends Activity {
int mode = 0;
int id = 0;
EditText edt1=null;
EditText edt2=null;
EditText edt3=null;
EditText edt4=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_memo_editor);
edt1 = (EditText) findViewById(R.id.memoId);
edt2 = (EditText) findViewById(R.id.title);
edt3 = (EditText) findViewById(R.id.content);
edt4 = (EditText) findViewById(R.id.date);
Intent intent = getIntent();
mode = intent.getIntExtra("mode", 0);
if (mode == 1) {
id = intent.getIntExtra("id", -1); //write random number that doesn't make any sense
if(id!= -1){
MemoItem item = MemoItemMgr.getInstance().search(id);
edt1.setText(item.getMemoId() + "");
edt1.setEnabled(false); //this will prevent you from editing
edt2.setText(item.getTitle());
edt3.setText(item.getContent());
edt4.setText(item.getDate());
}
}
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnQuit = (Button) findViewById(R.id.btnQuit);
btnQuit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quit(v);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save(v);
}
});
}
public void save(View v) {
int memoId = Integer.parseInt(edt1.getText().toString());
String title = edt2.getText().toString();
String content = edt3.getText().toString();
String date = edt4.getText().toString();
if (mode == 0) {
try {
MemoItemMgr.getInstance().add(new MemoItem(memoId, title, content, date));
finish();
} catch (DuplicateException e) {
Toast.makeText(this, "Duplicative ID. Please write a different ID", Toast.LENGTH_LONG).show();
edt1.setText("");
edt1.requestFocus();
}
} else if (mode == 1) {
MemoItemMgr.getInstance().update(id, title, content, date);
finish();
}
}
public void quit(View v) {
finish();
}
}
package com.example.mymemomain2;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class MemoClient extends Thread {
ArrayList list = null;
public MemoClient(ArrayList list){
this.list = list;
}
@Override
public void run() {
super.run();
ObjectOutputStream objOut = null;
Socket socket = null;
try {
socket = new Socket("70.12.109.92", 7777);
OutputStream out = socket.getOutputStream();
objOut = new ObjectOutputStream(out);
for(int i = 0; i < list.size(); i++){
objOut.writeObject(list.get(i));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objOut.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.EOFException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import com.example.mymemomain2.MemoItem;
public class MemoServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(7777);
while (true) {
System.out.println("waiting...");
Socket socket = server.accept();
InputStream in = socket.getInputStream();
ObjectInputStream objIn = new ObjectInputStream(in);
while (true) {
try {
MemoItem item = (MemoItem) objIn.readObject();
System.out.println(item);
} catch (EOFException e) {
break;
}
}
}
}
}
*******************************************************************************
********************Just FYI, here is another example.**************************
*******************************************************************************
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NotePadActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NoteEditActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edtId"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edtTitle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edtContent"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save"
android:id="@+id/btnSave"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:id="@+id/btnCancel"
/>
</LinearLayout>
</LinearLayout>
package com.example.notepad;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class NotePadActivity extends Activity {
public static final int MODE_ADD = 0;
public static final int MODE_EDIT = 1;
public static final int MENU_ADD = 1;
public static final int MENU_EDIT = 2;
public static final int MENU_DELETE = 3;
public static final int MENU_SEND = 4;
public static final int MENU_EXIT = 5;
ListView listview = null;
ArrayAdapter<Note> adt = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NoteManager.getInstance().open(this);
adt = new ArrayAdapter<Note>(this,
android.R.layout.simple_list_item_single_choice, NoteManager
.getInstance().getList());
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adt);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_ADD, 0, "Add");
menu.add(0, MENU_EDIT, 0, "Edit");
menu.add(0, MENU_DELETE, 0, "Delete");
menu.add(0, MENU_SEND, 0, "Send");
menu.add(0, MENU_EXIT, 0, "Exit");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == MENU_ADD) {
add();
} else if (item.getItemId() == MENU_EDIT) {
edit();
} else if (item.getItemId() == MENU_DELETE) {
delete();
} else if (item.getItemId() == MENU_SEND) {
send();
} else if (item.getItemId() == MENU_EXIT) {
exit();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
NoteManager.getInstance().close(this);
}
private void exit() {
finish();
}
private void send() {
NoteClient client = new NoteClient(NoteManager.getInstance().getList());
client.start();
}
private void delete() {
int position = listview.getCheckedItemPosition();
if (position != -1) {
Note note = adt.getItem(position);
adt.remove(note);
} else {
Toast.makeText(this, "Please select the one that you want to delete", Toast.LENGTH_LONG).show();
}
}
private void edit() {
int position = listview.getCheckedItemPosition();
if (position != -1) {
Note note = adt.getItem(position);
int id = note.getId();
Intent intent = new Intent(this, NoteEditActivity.class);
intent.putExtra("mode", MODE_EDIT);
intent.putExtra("id", id);
startActivity(intent);
} else {
Toast.makeText(this, "Please select the one that you want to edit", Toast.LENGTH_LONG).show();
}
}
private void add() {
Intent intent = new Intent(this, NoteEditActivity.class);
intent.putExtra("mode", MODE_ADD);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
adt.notifyDataSetChanged();
}
}
package com.example.notepad;
import java.io.Serializable;
public class Note implements Serializable {
private int id;
private String title;
private String content;
public Note(int id, String title, String content) {
super();
this.id = id;
this.title = title;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "[" + id + "]" + title;
}
}
package com.example.notepad;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import android.content.Context;
public class NoteManager {
// need to use singleton
private static NoteManager instance = new NoteManager();
public static NoteManager getInstance(){
return instance;
}
private String fileName = "note.ser";
private ArrayList<Note> list = new ArrayList<Note>();
private NoteManager(){
}
public void open(Context ctx){
File file = new File(ctx.getFilesDir(), fileName);
if(file.exists()){
//use FileStream to read the file and add by using add()
ObjectInputStream objIn = null;
try {
FileInputStream fin = ctx.openFileInput(fileName);
objIn = new ObjectInputStream(fin);
while(true){
try{
Note note = (Note)objIn.readObject();
add(note);
}catch(EOFException e){
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
//force add the content by using add()
try {
add(new Note(123, "Ethical quotes", "Content blah blah"));
add(new Note(124, "Todo", "bla she bla she"));
add(new Note(1, "Grocery", "hohoho"));
add(new Note(13, "Bus route", "hong hong hong"));
} catch (DuplicateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void close(Context ctx){
ObjectOutputStream objOut = null;
try {
FileOutputStream fin = ctx.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE);
objOut = new ObjectOutputStream(fin);
for(int i = 0; i < list.size(); i++){
Note note = list.get(i);
objOut.writeObject(note);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
objOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void add(Note note) throws DuplicateException{
//repetition check
if(search(note.getId()) == null){
list.add(note);
}else{
throw new DuplicateException();
}
}
public Note search(int id){
for (int i = 0; i < list.size(); i++) {
Note note = list.get(i);
if(id == note.getId()){
return note;
}
}
return null;
}
public ArrayList<Note> getList(){
return list;
}
public void update(int id, String title, String content){
Note note = search(id);
note.setTitle(title);
note.setContent(content);
}
public boolean remove(int id){
Note note = search(id);
return list.remove(note);
}
}
package com.example.notepad;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class NoteEditActivity extends Activity {
int mode = 0;
EditText edtId;
EditText edtTitle;
EditText edtContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_edit);
edtId = (EditText) findViewById(R.id.edtId);
edtTitle = (EditText) findViewById(R.id.edtTitle);
edtContent = (EditText) findViewById(R.id.edtContent);
Intent intent = getIntent();
mode = intent.getIntExtra("mode", NotePadActivity.MODE_ADD);
if (mode == NotePadActivity.MODE_EDIT) {
edtId.setEnabled(false);
int id = intent.getIntExtra("id", -1);
Note note = NoteManager.getInstance().search(id);
edtId.setText(id + "");
edtTitle.setText(note.getTitle());
edtContent.setText(note.getContent());
}
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnCancel = (Button) findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = Integer.parseInt(edtId.getText().toString());
String title = edtTitle.getText().toString();
String content = edtContent.getText().toString();
if (mode == NotePadActivity.MODE_ADD) {
try {
NoteManager.getInstance().add(
new Note(id, title, content));
finish();
} catch (DuplicateException e) {
Toast.makeText(NoteEditActivity.this,
"Duplicate ID, please use different ID", Toast.LENGTH_LONG)
.show();
}
} else if (mode == NotePadActivity.MODE_EDIT) {
NoteManager.getInstance().update(id, title, content);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.note_edit, menu);
return true;
}
}
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class NoteServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8888);
System.out.println("waiting....");
while(true){
Socket socket = server.accept();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
while(true){
try{
// Note note = in.readObject();
String note = (String)in.readObject(); //use this if you don't remember.
System.out.println(note);
}catch(EOFException e){
break;
}
}
}
}
}
package com.example.notepad;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
public class NoteClient extends Thread {
ArrayList<Note> list = null;
public NoteClient(ArrayList<Note> list){
this.list = list;
}
@Override
public void run() {
super.run();
Socket socket = null;
ObjectOutputStream out = null;
try{
socket = new Socket("70.12.109.92", 8888);
out = new ObjectOutputStream(socket.getOutputStream());
for (int i = 0; i < list.size(); i++) {
Note note = list.get(i);
// out.writeObject(note);
out.writeObject(note.toString()); //if used toString in Server
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}