我正在尝试学习Android来龙去脉,并且发现了一些真正的挑战。 我试图将Button链接到TextView ,当您单击Button时, TextView的值会更改。 我尝试使用一些我在网上找到的示例代码,但遇到很多错误,而且不确定如何处理。 我会很感激您能提供的任何帮助。

非常感谢你的帮助!

在第25行:

Multiple markers at this line
- main cannot be resolved or is not a field
- R cannot be resolved to a variable

在第29行:

btn1 cannot be resolved or is not a field

在第32行:

Syntax error on token(s), misplaced construct(s)

在第34行:

Multiple markers at this line
- Syntax error on token ")", ; expected
- Syntax error on token "(", ; expected
- void is an invalid type for the variable onClick

在第36行:

txtVw1 cannot be resolved or is not a field

然后这是我的代码:

package com.demuro1.views;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item);
}

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

Button aBut = (Button) findViewById(R.id.btn1);

public void changeText(){
    mButton.setOnClickListener(new View.OnClickListener {
        public void onClick(View v) {
            final TextView mTextView = (TextView) findViewById(R.id.txtVw1);
            mTextView.setText("Some Text");
        }
    };
}

}

然后从视图中的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    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=".MainActivity" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="129dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txtVw1"
                    android:textSize="25sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="press button 1" />

                <Button
                    android:id="@+id/btn1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="Button 1" 
                    android:onClick="activateBtn1"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="140dp"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txtVw2"
                    android:textSize="25sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="press button 2" />

                <Button
                    android:id="@+id/btn2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 2" 
                    android:onClick="activateBtn2"/>
                    />
            </LinearLayout>
        </LinearLayout>

</LinearLayout>

===============>>#1 票数:2

  1. 您不需要导入android.R
  2. main无法解析或不是字段 :确保在res / menu文件夹中有一个名为main.xml的文件
  3. btn1 cannot be resolved or is not a field :设置内容后在onCreate()初始化btn1
  4. Syntax error on token(s), misplaced construct(s)放置Syntax error on token(s), misplaced construct(s) :在onCreate内设置按钮的单击侦听器
  5. txtVw1 cannot be resolved or is not a field :与3中相同

像这样更改您的MainActivity.java

package com.demuro1.views;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView mTextView;
Button aBut;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView) findViewById(R.id.txtVw1);
    aBut = (Button) findViewById(R.id.btn1);
    aBut.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mTextView.setText("Some Text");
        }
    });

    // Do similar thing with TextView (R.id.txtVw2) and Button (R.id.btn2) here
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

===============>>#2 票数:2 已采纳

使用以下代码

package com.demuro1.views;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button aBut = (Button)findViewById(R.id.btn1);
    Button bBut = (Button)findViewById(R.id.btn2);
    final TextView aText = (TextView)findViewById(R.id.txtVw1);
    final TextView bText = (TextView)findViewById(R.id.txtVw2);

    aBut.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            aText.setText("You Clicked Button 1");

        }
    });

    bBut.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            bText.setText("You Clicked Button 2");

        }
    });
}

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

   }

  ask by demuro1 translate from so

未解决问题?本站智能推荐: