注意:我是 React Native 的新手。 下面的代码应该是使用 React Native 运行的计算器。我在显示此计算器代码的按钮时遇到问题。代码运行时没有错误,所以我不明白为什么没有出现按钮。
代码如下:
import React, { Component } from 'react';
import {
View,
Text,
AppRegistry,
StyleSheet,
} from 'react-native';
const inputButtons = [
[1, 2, 3, '/'],
[4, 5, 6, '*'],
[7, 8, 9, '-'],
[0, '.', '=', '+']
];
const Style = StyleSheet.create({
rootContainer: {
flex: 1
},
displayContainer: {
flex: 2,
backgroundColor: '#193441'
},
inputContainer: {
flex: 8,
backgroundColor: '#3E606F'
},
inputButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 0.5,
borderColor: '#91AA9D'
},
inputButtonText: {
fontSize: 22,
fontWeight: 'bold',
color: 'white'
},
inputRow: {
flex: 1,
flexDirection: 'row'
}
});
<View style={Style.rootContainer}>
<View style={Style.displayContainer}></View>
<View style={Style.inputContainer}></View>
</View>
export default class ReactCalculator extends Component {
render() {
return (
<View style={Style.rootContainer}>
<View style={Style.displayContainer}></View>
<View style={Style.inputContainer}>
{this._renderInputButtons()}
</View>
</View>
)
}
_renderInputButtons() {
let views = [];
for (var r = 0; r < inputButtons.length; r ++) {
let row = inputButtons[r];
let inputRow = [];
for (var i = 0; i < row.length; i ++) {
let input = row[i];
inputRow.push(
<InputButton value={input} key={r + "-" + i} />
);
}
views.push(<View style={Style.inputRow} key={"row-" + r}>{inputRow}</View>)
}
return views;
}
render() {
return (
<View style={{flex: 1}}>
<View style={{flex: 2, backgroundColor: '#193441'}}></View>
<View style={{flex: 8, backgroundColor: '#3E606F'}}></View>
</View>
)
}
}
最新代码: - 当我没有收到错误时收到错误,按钮出现在不正确的位置。
import React, { Component } from 'react';
import {
View,
Text,
AppRegistry,
StyleSheet,
Button,
TouchableHighlight,
} from 'react-native';
const inputButton = [
[1, 2, 3, '/'],
[4, 5, 6, '*'],
[7, 8, 9, '-'],
[0, '.', '=', '+']
];
const Style = StyleSheet.create({
rootContainer: {
flex: 1
},
displayContainer: {
flex: 2,
backgroundColor: '#193441'
},
inputContainer: {
flex: 8,
backgroundColor: '#3E606F'
},
inputButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 0.5,
borderColor: '#91AA9D'
},
inputButtonText: {
fontSize: 22,
fontWeight: 'bold',
color: 'white'
},
inputRow: {
flex: 1,
flexDirection: 'row'
}
});
<View style={Style.rootContainer}>
<View style={Style.displayContainer}></View>
<View style={Style.inputContainer}></View>
</View>
export default class ReactCalculator extends Component {
render() {
return (
<TouchableHighlight style={Style.inputButton}
underlayColor="#193441"
onPress={this.props.onPress}>
<Text style={Style.inputButtonText}>{this.props.value}</Text>
</TouchableHighlight>
)
}
_renderInputButton() {
let views = [];
for (var r = 0; r < inputButton.length; r ++) {
let row = inputButton[r];
let inputRow = [];
for (var i = 0; i < row.length; i ++) {
let input = row[i].toString();
inputRow.push(
<InputButton
value={input}
onPress={this._onInputButtonPressed.bind(this, input)}
key={r + "-" + i}/>
);
}
_onInputButtonPressed(input) {
alert(input)
}
views.push(<View style={Style.inputRow} key={"row-" + r}>{inputRow}</View>)
}
return views;
}
}
最佳答案
在你的代码中我发现了一些问题:
1. 方法 this._renderInputButton() 未定义,因为当您声明该方法时,您编写了 _renderinputButton()。您必须调用具有相同名称的方法。 (React Native 区分大小写)
2. 我没有在您的代码中找到组件 InputButton。如何创建组件?
我认为是您的代码无法正常工作的问题。也许你可以告诉我你从哪里获得代码。
编辑#1
您可以创建与文件 index.js 分开的 InputButton。然后在文件 InputButton.js 中写入:
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet
} from 'react-native';
const Style = StyleSheet.create({
inputButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 0.5,
borderColor: '#91AA9D',
},
inputButtonText: {
fontSize: 22,
fontWeight: 'bold',
color: 'white',
},
});
export default class InputButton extends Component {
render() {
return (
<View style={Style.inputButton}>
<Text style={Style.inputButtonText}>{this.props.value}</Text>
</View>
)
}
}
并且您可以在文件 index.js 中添加 import InputButton from './InputButton',例如:
import React, { Component } from 'react';
import { View, Text, AppRegistry, StyleSheet } from 'react-native';
import InputButton from './InputButton';
const inputButton = [
[1, 2, 3, '/'],
[4, 5, 6, '*'],
[7, 8, 9, '-'],
[0, '.', '=', '+'],
];
const Style = StyleSheet.create({
rootContainer: {
flex: 1,
},
displayContainer: {
flex: 2,
backgroundColor: '#193441',
},
inputContainer: {
flex: 8,
backgroundColor: '#3E606F',
},
inputButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 0.5,
borderColor: '#91AA9D',
},
inputButtonText: {
fontSize: 22,
fontWeight: 'bold',
color: 'white',
},
inputRow: {
flex: 1,
flexDirection: 'row',
},
});
export default class routerFlax extends Component {
_renderInputButton() {
let views = [];
for (var r = 0; r < inputButton.length; r++) {
let row = inputButton[r];
let inputRow = [];
for (var i = 0; i < row.length; i++) {
let input = row[i];
inputRow.push(<InputButton value={input} key={r + '-' + i} />);
}
views.push(
<View style={Style.inputRow} key={'row-' + r}>{inputRow}</View>,
);
}
return views;
}
render() {
return (
<View style={Style.rootContainer}>
<View style={Style.displayContainer} />
<View style={Style.inputContainer}>
{this._renderInputButton()}
</View>
</View>
);
}
}
埃迪#2
如果你想在一个文件中声明,你可以按照下面的代码:
import React, { Component } from 'react';
import { View, Text, AppRegistry, StyleSheet, TouchableOpacity } from 'react-native';
const inputButton = [
[1, 2, 3, '/'],
[4, 5, 6, '*'],
[7, 8, 9, '-'],
[0, '.', '=', '+'],
];
const Style = StyleSheet.create({
rootContainer: {
flex: 1,
},
displayContainer: {
flex: 2,
backgroundColor: '#193441',
},
inputContainer: {
flex: 8,
backgroundColor: '#3E606F',
},
inputButton: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 0.5,
borderColor: '#91AA9D',
},
inputButtonText: {
fontSize: 22,
fontWeight: 'bold',
color: 'white',
},
inputRow: {
flex: 1,
flexDirection: 'row',
},
});
const InputButton = ({value}) => {
return (
<View style={Style.inputButton}>
<Text style={Style.inputButtonText}>{value}</Text>
</View>
)
}
export default class routerFlax extends Component {
_renderInputButton() {
let views = [];
for (var r = 0; r < inputButton.length; r++) {
let row = inputButton[r];
let inputRow = [];
for (var i = 0; i < row.length; i++) {
let input = row[i];
inputRow.push(<InputButton value={input} key={r + '-' + i} />);
}
views.push(
<View style={Style.inputRow} key={'row-' + r}>{inputRow}</View>
);
}
return views;
}
render() {
return (
<View style={Style.rootContainer}>
<View style={Style.displayContainer} />
<View style={Style.inputContainer}>
{this._renderInputButton()}
</View>
</View>
);
}
}
刚刚添加了组件InputButton,例如:
const InputButton = ({value}) => {
return (
<View style={Style.inputButton}>
<Text style={Style.inputButtonText}>{value}</Text>
</View>
)
}
在我的模拟器中,这段代码的工作方式如下:
希望我的回答能给您思路并解决您的问题。快乐编码!
关于javascript - 按钮没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45154539/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象
我在非Rails项目中使用ActiveRecord。在Rails中,我可以这样做:config.time_zone='EasternTime(US&Canada)'config.active_record.default_timezone='EasternTime(US&Canada)'但如果我不使用rails,我该如何设置时区? 最佳答案 ActiveRecord::Base.default_timezone='EasternTime(US&Canada)' 关于ruby-没有轨道的A
在我让另一个人重做我的前端UI之前,我的Rails应用程序运行平稳。我已经尝试解决此错误3天了。这是错误:Nosuchfileordirectory-identifyExtractedsource(aroundline#59):575859606162@post=Post.find(params[:id])authorize@postif@post.update_attributes(post_params)flash[:notice]="Postwasupdated."redirect_to[@topic,@post]else{"utf8"=>"✓","_method"=>"patc
我正在尝试在配备ARMv7处理器的SynologyDS215j上安装ruby2.2.4或2.3.0。我用了optware-ng安装gcc、make、openssl、openssl-dev和zlib。我根据README中的说明安装了rbenv(版本1.0.0-19-g29b4da7)和ruby-build插件。.这些是随optware-ng安装的软件包及其版本binutils-2.25.1-1gcc-5.3.0-6gconv-modules-2.21-3glibc-opt-2.21-4libc-dev-2.21-1libgmp-6.0.0a-1libmpc-1.0.2-1libm