问题
单击按钮时,不会触发此代码。
mBtnSend = (Button) findViewById(R.id.btnSend);
mBtnSend.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
mTest= (EditText) findViewById(R.id.txtText);
mTest.setText("hello");
}
});
之前的代码在里面:
@Override
public void onCreate(Bundle savedInstanceState) {
在 setContentView(R.layout.main);
之后的 MainActivity.java 中按钮和EditText的两个变量都在这里声明:
public class MainActivity extends Activity implements IToolbarsContainer, OnTouchListener, IDownloadEventsListener {
private Button mBotonEnviar;
private EditText mCampoPrueba;
我有:
import android.view.View.OnClickListener;
这是完整的布局
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="org.cade.codigos.ui.activities.RequestData">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Titulo"
android:id="@+id/txtTitiulo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Nombre"
android:id="@+id/lblNombre"
android:layout_marginTop="40dp"
android:layout_below="@+id/txtTitiulo"
android:layout_alignParentRight="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Nombre"
android:ems="10"
android:id="@+id/txtNombre"
android:layout_below="@+id/lblNombre"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Apellido"
android:id="@+id/lblApellido"
android:layout_below="@+id/txtText"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Apellido"
android:ems="10"
android:id="@+id/txtApellido"
android:layout_below="@+id/lblApellido"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/btnSend"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
长... onCreate请求方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
INSTANCE = this;
Constants.initializeConstantsFromResources(this);
Controller.getInstance().setPreferences(PreferenceManager.getDefaultSharedPreferences(this));
if (Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_SHOW_FULL_SCREEN, false)) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
if (Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_GENERAL_HIDE_TITLE_BARS, true)) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
setProgressBarVisibility(true);
//setContentView(R.layout.main);
setContentView(R.layout.activity_request_data);
mBotonEnviar = (Button) findViewById(R.id.btnEnviar);
mBotonEnviar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
mCampoPrueba = (EditText) findViewById(R.id.txtApellido);
mCampoPrueba.setText("hola");
}
});
mCircularProgress = getResources().getDrawable(R.drawable.spinner);
EventController.getInstance().addDownloadListener(this);
mHideToolbarsRunnable = null;
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
buildComponents();
mViewFlipper.removeAllViews();
updateSwitchTabsMethod();
updateBookmarksDatabaseSource();
registerPreferenceChangeListener();
Intent i = getIntent();
if (i.getData() != null) {
// App first launch from another app.
addTab(false);
navigateToUrl(i.getDataString());
} else {
// Normal start.
int currentVersionCode = ApplicationUtils.getApplicationVersionCode(this);
int savedVersionCode = PreferenceManager.getDefaultSharedPreferences(this).getInt(Constants.PREFERENCES_LAST_VERSION_CODE, -1);
// If currentVersionCode and savedVersionCode are different, the application has been updated.
if (currentVersionCode != savedVersionCode) {
// Save current version code.
Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putInt(Constants.PREFERENCES_LAST_VERSION_CODE, currentVersionCode);
editor.commit();
// Display changelog dialog.
Intent changelogIntent = new Intent(this, ChangelogActivity.class);
startActivity(changelogIntent);
}
/*
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREFERENCES_BROWSER_RESTORE_LAST_PAGE, false)) {
if (savedInstanceState != null) {
String savedUrl = savedInstanceState.getString(Constants.EXTRA_SAVED_URL);
if (savedUrl != null) {
addTab(false);
navigateToUrl(savedUrl);
lastPageRestored = true;
}
}
}*/
boolean lastPageRestored = false;
// gaf - comienzo con la pagina inicial
String savedUrl = "file:///android_asset/startpage/index.html";
//String savedUrl = "http://servidor-cade.com";
//String savedUrl = "http://192.168.1.18:3636";
addTab(false);
navigateToUrl(savedUrl);
lastPageRestored = false;
//if (!lastPageRestored) {
// addTab(true);
//}
}
initializeWebIconDatabase();
startToolbarsHideRunnable();
}
答案 0 :(得分:0)
btnEnviar在哪里? 拿findViewById(R.id.txtApellido);走出听众。 也许试试这个:
mCampoPrueba = (EditText) findViewById(R.id.txtApellido);
mBotonEnviar = (Button) findViewById(R.id.btnEnviar);
mBotonEnviar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
mCampoPrueba.setText("hola");
}
});
答案 1 :(得分:0)
我认为你必须在onCreate方法中初始化mTest,因为当你创建一个监听器时,你创建了新的anonymus对象。如果您想在MainActivity中转换为对象,则必须调用:
mTest= (EditText) MainActivity.this.findViewById(R.id.txtText)
答案 2 :(得分:0)
布局文件没有R.id.btnEnviar
的按钮。您的意思是:R.id.btnSend
mCampoPrueba = (EditText) findViewById(R.id.txtApellido);
mBotonEnviar = (Button) findViewById(R.id.btnSend);
mBotonEnviar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
mCampoPrueba.setText("hola");
}
});
答案 3 :(得分:0)
问题是由于我缺乏知识,我在错误的活动中编写代码,非常感谢大家!