安卓是否可以从未连接到xml的xml活动接收到java的EditText?
我有一个XML,上面有一个编辑文本,一个连接到它的java文件,它可以接收编辑文本并转换成字符串,没有问题
XML
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent" 安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="10dp"
安卓:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.coopery.notes.NoteActivity"
安卓:background="#da141415">
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/etTitle"
安卓:layout_alignParentStart="true"
安卓:inputType="textCapSentences|textAutoCorrect"
安卓:imeOptions="actionDone"
安卓:hint="Title..."
安卓:textSize="17sp"
安卓:textColor="#ffffffff" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:inputType="textMultiLine|textCapSentences|textAutoCorrect"
安卓:scrollbars="vertical"
安卓:ems="10"
安卓:id="@+id/etNote"
安卓:layout_below="@id/etTitle"
安卓:layout_alignParentStart="true"
安卓:gravity="top"
安卓:background="@null"
安卓:hint="Note..."
安卓:textSize="17sp"
安卓:textColor="#ffffffff" />
Java
public class NoteActivity extends Activity {
Typeface customFont;
EditText etTitle;
EditText etNote;
String oldTitle;
AlarmPage A = new AlarmPage();
Notification.Builder notification;
NotificationManager manager;
private String Title, Note1, Note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
String fileName = getIntent().getStringExtra(MainActivity.EXTRA_TITLE);
oldTitle = fileName;
etTitle = (EditText) findViewById(R.id.etTitle);
etNote = (EditText) findViewById(R.id.etNote);
String Title = etTitle.getText().toString();
String Note = etNote.getText().toString();
但是另一个java文件,当我试图得到相同的代码时,它是一个空的。我怎么做呢
如果我不够具体,请让我知道,这是一个有点难问的问题
这里是另一个java文件中的方法,我正在尝试使用它并接收edittext
public void notification() {
setContentView(R.layout.activity_note);
EditText TitleE, NoteE;
TitleE = (EditText) findViewById(R.id.etTitle);
NoteE = (EditText)findViewById(R.id.etNote);
String Title, Note;
Title = TitleE.getText().toString();
Note = NoteE.getText().toString();
//both PingService and CommonConstants java files are needed for this method
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(Title)
.setContentText(Note)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE
.setStyle(new NotificationCompat.BigTextStyle().bigText("hi"));
注意:我放置了第二个setContentView以查看它是否有效,但没有
# 1 楼答案
像这样修改授权和恩诺特声明,这将使它们在您的所有活动中都是全局的:
然后,您将能够从其他活动中获取它们的值,但是如果您尝试从其他活动中更改它们,您将得到一个异常
如果其他人将修改您的代码,最好将这些变量设置为私有,并创建两个方法:
# 2 楼答案
在类A中将edittext声明为公共静态
公共文本标题
现在您可以在B类中访问此edittext
A.头衔