点击Android的一个按钮时,显示的字符串的TextView的 [英] displaying a string on the textview when clicking a button in android

查看:336
本文介绍了点击Android的一个按钮时,显示的字符串的TextView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是很新的Andr​​oid开发刚开始学习。

我想要是添加一个按钮,当按钮是pressed创文我的第一个项目即可显示在文本视图。

通过一些专家的帮助下,我创建的按钮和文本视图。

使按键显示在模拟器但是当我点击该按钮没有任何反应。

任何人都可以请帮助我我怎么可以显示文本时pressing按钮?

的.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的TextView
        机器人:ID =@ + ID / txtView
        机器人:layout_width =150dp
        机器人:layout_height =150dp
        机器人:文本=@字符串/你好/>

    <按钮
   机器人:ID =@ + ID / mybtn
   机器人:layout_width =50dp
   机器人:layout_height =30dp/>
    <的TextView
   机器人:ID =@ + ID / viewwidth
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =WRAP_CONTENT/>
<的TextView
   机器人:ID =@ + ID / viewheight
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>
 

的.java

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.Button;
进口android.widget.TextView;
进口android.view.View;
进口android.widget.Toast;

公共类NameonbuttonclickActivity扩展活动实现View.OnClickListener {
    按钮mybtn;
    TextView的txtView;
    字符串Hello;
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        的setContentView(R.layout.main);
        super.onCreate(savedInstanceState);
        mybtn =新的按钮(这一点);
        txtView =新的TextView(本);
        mybtn.setOnClickListener(本);
        printmyname();

        mybtn =(按钮)findViewById(R.id.mybtn);
        txtView =(TextView中)findViewById(R.id.txtView);
        txtView =(TextView中)findViewById(R.id.viewwidth);
        txtView =(TextView中)findViewById(R.id.viewheight);

        你好=这是我的第一个项目;

        //的setContentView(mybtn);
       //的setContentView(R.layout.main);
    }

    公共无效的onClick(视图查看){
        txtView.setText(你好);

    // printmyname();
     Toast.makeText(NameonbuttonclickActivity.this,你好,Toast.LENGTH_LONG).show();

    }
    私人无效printmyname(){
        的System.out.println(来了);

    }
}
 

解决方案

您可以这样做:

 字符串Hello;
公共无效的onCreate(包savedInstanceState){
    的setContentView(R.layout.main);
    super.onCreate(savedInstanceState);

    mybtn =(按钮)findViewById(R.id.mybtn);
    txtView =(TextView中)findViewById(R.id.txtView);
    txtwidth =(TextView中)findViewById(R.id.viewwidth);
    你好=这是我的第一个项目;


    mybtn.setOnClickListener(本);
}
公共无效的onClick(视图查看){
    txtView.setText(你好);
}
 

请检查您的TextView的名字。两者都是一样的。您必须使用不同的对象的名字和你所提到的TextView的对象,它是不是在你的XML布局文件中。 希望这会帮助你。

I'm very new to Android development and just started to study.

What I'm trying is to add a button and when that button is pressed a text "my first project" to get displayed in the text view.

With the help of some experts I created the button and text view.

So the button is showing in the simulator but when I click that button nothing happens.

Can anyone please help me with how can I display the text when pressing the button?

.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>

.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");       

    }
}

解决方案

You can do like this:

 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);
}

Check your textview names. Both are same . You must use different object names and you have mentioned that textview object which is not available in your xml layout file. Hope this will help you.

这篇关于点击Android的一个按钮时,显示的字符串的TextView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆