周末撸了几次代码,
舍不得扔,
立此为照!
mainActivity.java
package com.tw.flag.ch08_memo;
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;
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
String[] aMemo = {
"1.单击可以编辑备忘",
"2.长按可以清除备忘", "3.", "4.", "5.", "6" };
ListView lv;
ArrayAdapter<String> aa;
@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_list_item_1, aMemo);
lv.setAdapter(aa);
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent it = new Intent(this, Edit.class);
it.putExtra("备忘", aMemo[position]);
startActivityForResult(it, position);
}
protected void onActivityResult(int requestCode, int resultCode, Intent it) {
if (resultCode == RESULT_OK) {
aMemo[requestCode] = it.getStringExtra("备忘");
aa.notifyDataSetChanged();
}
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
aMemo[position] = (position+1) + ".";
aa.notifyDataSetChanged();
return true;
}
}
Edit.java
package com.tw.flag.ch08_memo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Edit extends AppCompatActivity {
TextView txv;
EditText edt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
Intent it = getIntent();
String s = it.getStringExtra("备忘");
txv = (TextView)findViewById(R.id.textView);
txv.setText(s.substring(0, 2));
edt = (EditText)findViewById(R.id.editText);
if (s.length() > 3)
edt.setText(s.substring(3));
}
public void onCancel(View v) {
setResult(RESULT_CANCELED);
finish();
}
public void onSave(View v) {
Intent it2 = new Intent();
it2.putExtra("备忘", txv.getText() + " " + edt.getText());
setResult(RESULT_OK, it2);
finish();
}
}
你的样子: