我刚开始使用Android进行编程,但我无法通过点击我预期的按钮获得结果。这是代码。
.java文件
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class AndroidLove extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_love, menu);
return true;
}
public void onButtonClicked(View view) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
}
.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=".AndroidLove" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="104dp"
android:text="@string/hello_world"
android:visibility="invisible" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:text="@string/Button"
android:onClick="onButtonClicked" />
strings.xml文件
<resources>
<string name="app_name">AndroidLove</string>
<string name="action_settings">Settings</string>
<string name="hello_world">First Line\nSecond Line\nThird Line</string>
<string name="Button">Get Lyrics</string>
单击该按钮时,应显示以下文本但不显示: -
First Line
Second Line
Third Line
请帮忙
答案 0 :(得分:1)
编辑:或许你的绑定可能与onclick有关,请尝试下面的代码而不是top。将该代码放入onCreate
方法。
Button button= (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView=(TextView)findViewById(R.id.textView1);
textView.setVisibility(View.VISIBLE);
}
});
答案 1 :(得分:0)
您需要从strings.xml文件中获取可以通过
实现的字符串值String st =getResources.getString(R.string.hello_world);
然后通过
将其设置为textviewtextView.setText(st);
答案 2 :(得分:-1)
试试这段代码:
public class AndroidLove extends Activity {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_love);
textView=(TextView)findViewById(R.id.textView1);
button1 = (Button)findViewByid(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// do your stuff here
String _data =getResources.getString(R.string.hello_world);
textView.setText(_data);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}