基本上,我试图通过EditText
"确定"我的button
显示在TextView
输出中显示的内容。
一旦" Ok"按下button
,它会获取EditText
中输入的内容,并显示在TextView
中。
一直在寻找并找不到有效的方法。有人可以帮忙吗?
编辑: 1)真的很感激回应和帮助家伙。 2)我想我错了。 我尝试做的是创建一个选项,用户可以将自己的文本添加到活动中。
编辑: 谢谢你的帮助!!!!!!!!!!!!
答案 0 :(得分:5)
从EditText
editText.getText().toString();
在TextView
Button Click
textView.setText(editText.getText().toString());
实施Button Onclick(...)
之类的
Button negativeButton = (Button) findViewById(R.id.ButtonID);
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(editText.getText().toString());
}
});
答案 1 :(得分:2)
在Activity
OnCreate()
中使用
Button _ok = (Button) findViewById(R.id.idofButton);
TextView result = (TextView ) findViewById(R.id.idofTextView);
EditText entrytext = (EditText ) findViewById(R.id.idofEditText);
_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result .setText(entrytext .getText().toString());
}
});
答案 2 :(得分:0)
假设EditText命名为et
,TextView命名为tv
,而Button命名为bt
..
现在在你的类名下面声明你的EditText,TextView和Button,如下所示:
public class EditToText extends Activity{
EditText et;
TextView tv;
Button bt;
------
------
}
现在使用onCreate
方法,setContentView
之后执行以下操作:
------
------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.edittext); //EditText is defined as edittext in xml
tv = (TextView) findViewById(R.id.textview); //TextView is defined as textview in xml
bt = (Button) findViewById(R.id.button); //Button is defined as button in xml
//When button is clicked
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String txt = et.getText().toString(); //Get txt from et when button is clicked
tv.setText(txt); //Set text extracted from et in tv
}
});
}
------
------
希望这有帮助!
答案 3 :(得分:-1)
activity_mail.xml
<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="com.example.stackedittext.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="ENTER TEXT HERE" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="62dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="42dp"
android:text="OK" />
</RelativeLayout>
MainActivity.java
package com.example.stackedittext;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button ok;
EditText edt;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button) findViewById(R.id.button1);
edt = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = edt.getText().toString();
tv.setText(msg);
// TODO Auto-generated method stub
}
});
}
@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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}