gpt4 book ai didi

java - Android 中的按钮和 TextView

转载 作者:搜寻专家 更新时间:2023-11-01 07:59:30 25 4
gpt4 key购买 nike

我正在尝试了解 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");
}
};
}

}

然后是 View 中的 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>

最佳答案

使用下面的代码

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

}

关于java - Android 中的按钮和 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23176350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com