我正在放置三个文件1.activity_main.xml,2.MainActivity.java,3.strings.xml文件....我是一个新的应用程序开发人员,在使用Android应用程序时做了很多问题
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="faizsols.com.test.MainActivity">
<TextView
android:text="@string/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="134dp"
android:layout_marginTop="106dp"
android:id="@+id/text1"
android:textSize="30sp" />
<Button
android:text="@string/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_alignStart="@+id/text1"
android:layout_marginTop="99dp"
android:onClick="rep"
android:id="@+id/but1" />
</RelativeLayout>
2.MainActivity.java
package faizsols.com.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView t1 = (TextView)findViewById(R.string.text1);
Button rpl = (Button)findViewById(R.string.but1);
public void rep(View view) {
t1.setText("I am fine");
}
}
3.strings.xml
<resources>
<string name="app_name">Test</string>
<string name="text1">hello</string>
<string name="but1">Reply</string>
</resources>
答案 0 :(得分:3)
使用此:
package faizsols.com.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// You should initialize in onCreate()
t1 = (TextView)findViewById(R.id.text1);
Button rpl = (Button)findViewById(R.id.but1);
}
public void rep(View view) {
t1.setText("I am fine");
}
答案 1 :(得分:1)
将这些行放在setContentView
下面的onCreate中TextView t1;
Button rpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView)findViewById(R.id.text1);
rpl = (Button)findViewById(R.id.but1);
}
答案 2 :(得分:1)
Changes in MainActivity.class
package faizsols.com.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1 = (TextView)findViewById(R.id.text1);
Button rpl = (Button)findViewById(R.id.but1);
rpl .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t1.setText("I am fine");
}
});
}
答案 3 :(得分:0)
使用以下代码更新您的MainActivity
:
package faizsols.com.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
TextView t1;
Button rpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView)findViewById(R.id.text1);
rpl = (Button)findViewById(R.id.but1);
}
public void rep(View view) {
t1.setText("I am fine");
}
}
答案 4 :(得分:0)
在调用setConteview方法之后,你必须在oncreate()方法中获取id。
package faizsols.com.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
TextView t1;
Button rpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView)findViewById(R.id.text1);
rpl = (Button)findViewById(R.id.but1);
}
public void rep(View view) {
t1.setText("I am fine");
}
答案 5 :(得分:0)
请参阅以下内容:
TextView t1 = (TextView)findViewById(R.string.text1);
Button rpl = (Button)findViewById(R.string.but1);
这是错的。您需要撰写 R.id
,而不是R.string
。
TextView t1 = (TextView)findViewById(R.id.text1);
Button rpl = (Button)findViewById(R.id.but1);
使用此:
package faizsols.com.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
TextView t1 = (TextView)findViewById(R.id.text1);
Button rpl = (Button)findViewById(R.id.but1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void rep(View view) {
t1.setText("I am fine");
}
}
答案 6 :(得分:0)
我知道问题目前已经解决,但是我想补充一点,您可能会在最新版本的Android Studio中遇到问题。用TextView设置文本似乎是不可能的。另外,我一直在使用EditText,因为它易于使用,并且可以在任何函数中调用和定义(不仅限于OnCreate)。
编辑文本可以在设计布局下的“文本”下的调色板选项中找到,其用法与TextView非常相似
EditText txt =(EditText) findViewById(R.id.editText);
txt.setText("This actually works");