我对Android开发很新,刚开始学习.
我正在尝试的是添加一个按钮,当按下该按钮时,文本"我的第一个项目"将显示在文本视图中.
在一些专家的帮助下,我创建了按钮和文本视图.
所以按钮显示在模拟器中,但是当我点击该按钮时没有任何反应.
任何人都可以请帮助我如何在按下按钮时显示文字?
.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtView"
android:layout_width="150dp"
android:layout_height="150dp"
android:text="@string/hello" />
<Button
android:id="@+id/mybtn"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/viewwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/viewheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
的.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
String hello;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn= new Button(this);
txtView=new TextView(this);
mybtn.setOnClickListener(this);
printmyname();
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtView = (TextView)findViewById(R.id.viewwidth);
txtView = (TextView)findViewById(R.id.viewheight);
hello="This is my first project";
//setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
txtView.setText(hello);
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, hello, Toast.LENGTH_LONG).show();
}
private void printmyname(){
System.out.println("coming");
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
String hello;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtwidth = (TextView)findViewById(R.id.viewwidth);
hello="This is my first project";
mybtn.setOnClickListener(this);
}
public void onClick(View view){
txtView.setText(hello);
}
Run Code Online (Sandbox Code Playgroud)
检查您的textview名称.两者都是一样的.您必须使用不同的对象名称,并且您已经提到了xml布局文件中不可用的textview对象.希望这会帮助你.
| 归档时间: |
|
| 查看次数: |
103427 次 |
| 最近记录: |