【问题标题】:displaying a string on the textview when clicking a button in android单击android中的按钮时在textview上显示一个字符串
【发布时间】:2016-02-06 04:18:13
【问题描述】:

我对 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>

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

    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以这样做:

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

    检查您的文本视图名称。两者都是一样的。您必须使用不同的对象名称,并且您已经提到了您的 xml 布局文件中不可用的 textview 对象。 希望这会帮助你。

    【讨论】:

    • 我工作了thankyou..当我使用两个文本视图名称相同时它工作。
    • ,你能告诉我Toast.makeText(NameonbuttonclickActivity.this, "hello", Toast.LENGTH_LONG).show()的功能是什么;
    • Toast() 只会在屏幕上创建一条警报消息一段时间,具体取决于您在 Toast() 中给出的长度。它需要上下文、要显示的字符串和吐司时间的长度
    • @druvisha.ok.如果我想更改按钮的标题我该怎么做。你能帮我吗。
    • 为您的按钮创建一个对象。假设这里是 mybtn。你可以写 mybtn.setText("New Button name");这一行你可以写在你的java文件中。并在 xml 布局文件中添加属性 android:text="Button name"
    【解决方案2】:

    首先创建xml文件如下。创建一个文本视图和一个按钮:

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
      <Button
        android:id="@+id/mybutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />
    
      <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    
    </LinearLayout>
    

    默认情况下会创建第一个 TextView。如果需要,您可以离开或删除它。 接下来是创建一个按钮 下一个是要显示文本的TextView。

    现在进入主要活动代码... 包 com.android.example.simple;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class SimpleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        final TextView textView=(TextView)findViewById(R.id.textView1);
        final Button button1 =  (Button)findViewById(R.id.mybutton);
    
        //Implement listener for your button so that when you click the 
        //button, android will listen to it.             
    
         button1.setOnClickListener(new View.OnClickListener() {             
            public void onClick(View v) {                 
            // Perform action on click 
                textView.setText("You clicked the button");
    
            }         });
        }
    }
    

    【讨论】:

    • 非常感谢朋友。它帮助并希望您将来也能提供帮助
    【解决方案3】:

    这是因为您不要在从 XML 布局检索的按钮上关联 OnClickListener()。

    您不需要创建对象,因为它们在您扩充 XML 文件时已由 Android 系统创建(使用 Activity.setContentLayout( int resourceLayoutId ) 方法)。

    只需使用 findViewById(...) 方法检索它们即可。

    【讨论】:

      【解决方案4】:

      只需检查 .java 类中的代码

      你在下面写了

       mybtn.setOnClickListener(this);
      

      我的意思是在初始化 mybtn 对象之前

      mybtn = (Button)findViewById(R.id.mybtn);
      

      在初始化您的 mybtn 对象后,只需切换这两行或输入那一行“mybtn.setOnClickListener(this)”,您就会得到您想要的答案..

      【讨论】:

      • 现在总是遵循这个步骤,首先初始化所有对象,然后设置监听器或任何属性。如果我的回答对你有帮助,那么请接受作为答案..:)
      【解决方案5】:

      试试这个

      public void onClick(View view){
          txtView.setText("hello");
      
          //printmyname();
          Toast.makeText(NameonbuttonclickActivity.this, "hello", Toast.LENGTH_LONG).show();
      }
      

      在 toast 中也使用“Hello”

      【讨论】:

        【解决方案6】:

        你把它当作txtView.setText("hello");

        【讨论】:

        • ,点击按钮还是没有反应
        • 为xml中的按钮添加可点击属性
        【解决方案7】:

        MainActivity.java:

        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.widget.Button;
        import android.widget.ImageButton;
        import android.widget.ImageView;
        import android.widget.TextView;
        import android.widget.Toast;
        import android.view.View;
        import android.view.View.OnClickListener;
        
        public class MainActivity extends Activity {
        
            Button button1;
            TextView textView1;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        
                 button1=(Button)findViewById(R.id.button1);
                 textView1=(TextView)findViewById(R.id.textView1);
                button1.setOnClickListener(new View.OnClickListener() {
        
                    @Override
                    public void onClick(View v) {
        
                    textView1.setText("TextView displayed Successfully");
        
                    }
                });
        
        }
        
        }  
        

        activity_main.xml:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout 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:orientation="vertical" >
        
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Click here" />
        
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 />
        
        </LinearLayout>
        

        【讨论】:

          【解决方案8】:
          public void onCreate(Bundle savedInstanceState) 
          {
              setContentView(R.layout.main);
              super.onCreate(savedInstanceState);
          
              mybtn = (Button)findViewById(R.id.mybtn);
              txtView=(TextView)findViewById(R.id.txtView);
          
              mybtn .setOnClickListener(new OnClickListener() {
          
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      txtView.SetText("Your Message");
                  }
              });
          }
          

          【讨论】:

            【解决方案9】:

            检查一下:

            hello.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View paramView) {
                    text.setText("hello");
                }
            });
            

            【讨论】:

              猜你喜欢
              • 2012-11-27
              • 2016-09-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-14
              相关资源
              最近更新 更多