我正在创建一个简单的Android应用程序,包括3个活动。
第一个活动有一个按钮,使用意图显示listPage 在用户从列表中选择一个项目并单击确定后,系统会将他转移到包含所选项目将在其中显示的编辑框的第二个活动。 在用户点击“返回消息”按钮后,系统会将他转移到 MainActivity ,并在 Toast 中显示所选项目。
任何人都可以帮助我完成这项任务吗????
package com.devleb.intentmenudemoapp;
import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
TextView txt1, txt2;
EditText edittxt1, edittxt2;
Button btn1, btn2, btn3, btn4;
String msgStr, msgStr2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView) findViewById(R.id.txt1);
txt2 = (TextView) findViewById(R.id.txt2);
edittxt1 = (EditText) findViewById(R.id.edit1);
edittxt2 = (EditText) findViewById(R.id.edit2);
btn1 = (Button) findViewById(R.id.btn);
btn1.setOnClickListener(this);
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(this);
btn3 = (Button) findViewById(R.id.btn3);
btn3.setOnClickListener(this);
btn4 = (Button) findViewById(R.id.btn4);
btn4.setOnClickListener(this);
registerForContextMenu(edittxt1);
registerForContextMenu(edittxt2);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
msgStr = edittxt1.getText().toString();
msgStr2 = edittxt2.getText().toString();
switch (item.getItemId()) {
case R.id.bold_item:
edittxt1.setText(beautify(msgStr, "BOLD"));
edittxt2.setText(beautify(msgStr2, "BOLD"));
break;
case R.id.italic_item:
edittxt1.setText(beautify(msgStr, "ITALIC"));
edittxt2.setText(beautify(msgStr2, "ITALIC"));
break;
case R.id.red_item:
edittxt1.setTextColor(color.background_dark | Color.RED); // red
edittxt2.setTextColor(color.background_dark | Color.RED); // red
break;
case R.id.blue_item:
edittxt1.setTextColor(color.background_dark | Color.BLUE); // blue
edittxt2.setTextColor(color.background_dark | Color.BLUE); // blue
break;
case R.id.yellow_item:
edittxt1.setTextColor(color.background_dark | Color.YELLOW); // yellow
edittxt2.setTextColor(color.background_dark | Color.YELLOW); // yellow
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
msgStr = edittxt1.getText().toString();
msgStr2 = edittxt2.getText().toString();
switch (item.getItemId()) {
case R.id.bold_item:
edittxt1.setText(beautify(msgStr, "BOLD"));
edittxt2.setText(beautify(msgStr2, "BOLD"));
break;
case R.id.italic_item:
edittxt1.setText(beautify(msgStr, "ITALIC"));
edittxt2.setText(beautify(msgStr2, "ITALIC"));
break;
case R.id.red_item:
edittxt1.setTextColor(color.background_dark | Color.RED); // red
edittxt2.setTextColor(color.background_dark | Color.RED); // red
break;
case R.id.blue_item:
edittxt1.setTextColor(color.background_dark | Color.BLUE); // blue
edittxt2.setTextColor(color.background_dark | Color.BLUE); // blue
break;
case R.id.yellow_item:
edittxt1.setTextColor(color.background_dark | Color.YELLOW); // yellow
edittxt2.setTextColor(color.background_dark | Color.YELLOW); // yellow
break;
default:
break;
}
return super.onContextItemSelected(item);
}
private CharSequence beautify(String originalText, String selectedStyle) {
// TODO Auto-generated method stub
Spanned answer = null;
if (selectedStyle.equals("BOLD"))
answer = Html.fromHtml("<b>" + originalText + "</b>");
else if (selectedStyle.equals("ITALIC"))
answer = Html.fromHtml("<i>" + originalText + "</i>");
else if (selectedStyle.equals("NORMAL"))
answer = Html.fromHtml("<normal>" + originalText + "</normal>");
return answer;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String value;
switch (v.getId()) {
case R.id.btn:
value = edittxt1.getText().toString();
txt2.setText(value);
break;
case R.id.btn2:
// value = edittxt1.getText().toString();
Intent i = new Intent(this, ListPage.class);
//i.putExtra("msgFromFirstActivity", value);
startActivity(i);
break;
case R.id.btn3:
Intent ii = new Intent();
ii.setType("image/pictures/*");
ii.setAction(Intent.ACTION_GET_CONTENT);
startActivity(ii);
break;
case R.id.btn4:
// get back message from second activity
Intent iii = new Intent(this, SecondActivity.class);
startActivityForResult(iii, 1);
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (null != data) {
String returnMSG = data.getStringExtra("MESSAGE");
Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();
} else
Toast.makeText(this, "Error!!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
/*get back the result from the list page and display it in the edit
text box then after clicking show the result in the first activity as
toast
*/
package com.devleb.intentmenudemoapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends Activity {
TextView txt;
EditText edittxt;
String result;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
/*
Intent iList = new Intent(this, SecondActivity.class);
startActivityForResult(iList, 1);
*/
edittxt = (EditText) findViewById(R.id.edit_txt);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result = edittxt.getText().toString();
Intent i = new Intent();
i.putExtra("MESSAGE", result);
setResult(1, i);
finish();
}
});
}
/*@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (null != data) {
String returnMSG = data
.getStringExtra("MessageFromsecondActivity");
// Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();
edittxt.setText(returnMSG);
} else
Toast.makeText(this, "Error!!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
package com.devleb.intentmenudemoapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ListPage extends ListActivity {
TextView txt;
private static final String[] items = { "item1", "item2", "item3", "item4",
"item5", "item6", "item7", "item8", "item9" };
Button btn;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
txt = (TextView) findViewById(R.id.txt_list);
btn = (Button) findViewById(R.id.btn_list);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*
result = txt.getText().toString();
Intent i = new Intent();
i.putExtra("MessageFromsecondActivity", result);
setResult(1, i);
finish();
*/
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
txt.setText(items[position]);
super.onListItemClick(l, v, position, id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
}
直到现在这是我从这里做的如何继续或我需要添加和更改以完成我的任务
答案 0 :(得分:0)
你能做的是, 将数据从第一个活动传递到第二个活动
Intent in = new Intent(Youractivity.this, secondactivity.class);
in.putExtra("value", valuetopass);
startActivity(in);
现在,在第二个活动中获取数据
Intent in = getIntent();
String valuetoset = in.getStringExtra("value");
然后将此值设置为edittext
youredittext.setText(valuetoset);
答案 1 :(得分:0)
试试这个..
<强> ListPage.java 强>
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// TODO Auto-generated method stub
String val = ((TextView) v).getText().toString().trim();
txt.setText(items[position]);
Intent i = new Intent(ListPage.this, SecondActivity.class);
i.putExtra("value", val);
startActivity(in);
}
<强> SecondActivity.java 强>
Intent i = getIntent();
String val = "";
if(i.hasExtra("value"))
val = i.getStringExtra("value");
edittxt = (EditText) findViewById(R.id.edit_txt);
edittxt.setText(val);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result = edittxt.getText().toString();
Intent i = new Intent();
i.putExtra("MESSAGE", result);
startActivity(in);
}
});
<强> MainActivity.java 强>
Intent i = getIntent();
String value = "";
if(i.hasExtra("MESSAGE")){
value = i.getStringExtra("MESSAGE");
Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
}