我正在努力学习Android
的来龙去脉,我发现了一些真正的挑战。当您点击Button
更改TextView
中的值时,我正在尝试将Button
与TextView
相关联。我试过使用我在网上找到的一些示例代码,但是我遇到了很多错误,我不知道如何处理它们。我很感激您提供的任何帮助。
非常感谢你的帮助!
第25行:
Multiple markers at this line
- main cannot be resolved or is not a field
- R cannot be resolved to a variable
第29行:
btn1 cannot be resolved or is not a field
第32行:
Syntax error on token(s), misplaced construct(s)
第34行:
Multiple markers at this line
- Syntax error on token ")", ; expected
- Syntax error on token "(", ; expected
- void is an invalid type for the variable onClick
第36行:
txtVw1 cannot be resolved or is not a field
然后这是我的代码:
package com.demuro1.views;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
}
@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;
}
Button aBut = (Button) findViewById(R.id.btn1);
public void changeText(){
mButton.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.txtVw1);
mTextView.setText("Some Text");
}
};
}
}
然后从视图中找到xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/txtVw1"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press button 1" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button 1"
android:onClick="activateBtn1"/>
</LinearLayout>
<LinearLayout
android:layout_width="140dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/txtVw2"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press button 2" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:onClick="activateBtn2"/>
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
android.R
btn1 cannot be resolved or is not a field
:设置内容后在btn1
初始化onCreate()
Syntax error on token(s), misplaced construct(s)
:在onCreate
txtVw1 cannot be resolved or is not a field
:与3 像这样更改MainActivity.java
:
package com.demuro1.views;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTextView;
Button aBut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.txtVw1);
aBut = (Button) findViewById(R.id.btn1);
aBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTextView.setText("Some Text");
}
});
// Do similar thing with TextView (R.id.txtVw2) and Button (R.id.btn2) here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 1 :(得分:2)
使用以下代码
package com.demuro1.views;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button aBut = (Button)findViewById(R.id.btn1);
Button bBut = (Button)findViewById(R.id.btn2);
final TextView aText = (TextView)findViewById(R.id.txtVw1);
final TextView bText = (TextView)findViewById(R.id.txtVw2);
aBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
aText.setText("You Clicked Button 1");
}
});
bBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bText.setText("You Clicked Button 2");
}
});
}
@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;
}
}