【问题标题】:Displaying a button under a string message在字符串消息下显示按钮
【发布时间】:2015-05-09 17:41:00
【问题描述】:

您好,我对 android 编程非常陌生。我想获得一些有关在新激活的活动下的字符串消息下显示按钮的帮助。希望可以有人帮帮我。这是我在按下按钮应用教程时激活的 MyActivity 类的代码。我的主要活动的 xml 也在那里。 displaymessage 活动类和 xml 也在那里。

package com.example.sadi_ace.myfirstapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MyActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.sadi_ace.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_my);

        }


    /** Called when the user clicks the Send button */
    public void TutorialText(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        String message = "This is the android tutorial, if you want to pass on the to the " +
                "functions please press SKIP!";
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);



    }





  //  @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
  //      getMenuInflater().inflate(R.menu.menu_my, menu);
    //    return true;
  //  }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

主要活动的 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:orientation="vertical"
    android:background="#800080"
    android:weightSum="1">


    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/button_tutorial"
        android:background="#FFFFFF"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="TutorialText"
         />
</RelativeLayout>

DisplayMessageActivity 类代码

package com.example.sadi_ace.myfirstapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;


public class DisplayMessageActivity extends ActionBarActivity {




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);

        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }


    //  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_display_message, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

DisplayMessageActivity 类的 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:orientation="vertical"
    android:background="#800080"
    android:weightSum="1">



</RelativeLayout>

【问题讨论】:

    标签: android android-layout button android-activity mobile-devices


    【解决方案1】:

    在你的xml文件activity_my中添加按钮

       <Button
        android:id="@+id/btnskip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    之后在“onCreate”方法中使用这个按钮ID并将其分配给某个按钮

      Button skip;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    
        setContentView(R.layout.activity_my);
         skip=(Button)findViewById(R.id.btnskip);
    
      //add on click listener
        skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
             Intent intent = new Intent(this, DisplayMessageActivity.class);
        String message = "This is the android tutorial, if you want to pass on the to the " +
                "functions please press SKIP!";
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
        });
        }
    

    【讨论】:

    • 我正在使用安卓工作室。我不知道这是否会有很大的不同。它在 skip.setOnClickListener(new View.OnClickListener and this, DisplayMessageActivity.class 上显示错误
    • 在 Intent intent=new Intent(MyActivity.this,DisplayMessageActivity.class); 中使用“MyActivity.this”而不是仅“this”;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多